使用注解配置Spring AOP
来源:互联网 发布:叉叉助手最新源码 编辑:程序博客网 时间:2024/05/01 06:06
Spring框架提供通过注解方式配置的生命是SOP功能,是基于AspectJ实现的。
AspectJ提供整套AOP理论的完整实现,很多其他的AOP实现(包括其他语言)都借鉴了AspectJ的一些思想,AspectJ的很多实现方式已经成为AOP领域的事实标准。即使不使用Spring框架,也可直接使用AspectJ进行AOP编程,AspectJ允许使用注解来定义切面,切入点和增强,而Spring框架可以识别并根据这些注解生成AOP代理。
准备Aspect注解:
为了使用AspectJ注解方式,需要配置Spring AOP,首先要在Spring配置文件中声明<aop:aspectj-autoproxy>元素。同<aop:config>元素类似,<aop:aspectj-autoproxy>元素也具有proxy-target-class和expose-proxy属性。
配置切面说明:
实施增强的TransactionManager类需要配置为切面,AspectJ提供了下列注解用于配置切面,这些注解都位于org.aspectj.lang.annotation包下面。
●@Aspect:配置一个类为切面类
●@Before:配置一个方法为前置增强
●@After:配置一个方法为后置增强
●@AfterReturning:配置一个方法为返回后增强
●@AfterThrowing:配置一个方法为抛出异常后增强
●@Around:配置一个方法为环绕增强
●@DeclareParents:配置一个类为引介增强
下面通过例子进行演示:
(1)首先编写业务类,业务类GoodService,没有实现任何接口,代码如下:
<span style="font-size:18px;"><strong>package com.loster.li.spring_aop;public class GoodsService {private static int ORDER_NO = 1000; //用于生成订单编号public int buyGoods(String user,String productName){ORDER_NO++;System.out.println("【数据库插入订单】购买者"+user+",商品:"+productName+",订单编号:"+ORDER_NO);return ORDER_NO;}public void returnGoods(int orderNo){System.out.println("【数据库修改订单状态】订单编号:"+orderNo);}}</strong></span>(2)编写切面类,通过注解使事务管理类TransactionManager用于对业务类进行增强:
<span style="font-size:18px;"><strong>package com.loster.li.spring_aop;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class TransactionManager_AspectJ {@Before("execution(* com.loster.li.spring_aop.*Service.*(..))")public void beginTransaction(){System.out.println("【事务管理器】开始事务");}@AfterReturning("execution(* com.loster.li.spring_aop.*Service.*(..))")public void commit(){System.out.println("【事务管理器】提交事务");}@AfterThrowing("execution(* com.loster.li.spring_aop.*Service.*(..))")public void rollback(){System.out.println("【事务管理器】回滚事务");}}</strong></span>(3)在Spring配置文件中配置AOP,命名为applicationContext_AspectJ.xml,其代码如下:
<span style="font-size:18px;"><strong><?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- 业务类 --><bean id = "service" class = "com.loster.li.spring_aop.GoodsService"></bean><!-- 切面类 --><bean id = "transactionManager" class = "com.loster.li.spring_aop.TransactionManager_AspectJ"></bean><!-- 开启AspectJ注释 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans></strong></span>(4)编写测试类test2.java:
<span style="font-size:18px;"><strong>package com.loster.li.spring_aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class test2 {public static void main(String[] args){ApplicationContext context = new ClassPathXmlApplicationContext("/com/loster/li/spring_aop/applicationContext_AspectJ.xml");GoodsService service = (GoodsService)context.getBean("service");service.buyGoods("June", "Python"); //购买商品service.returnGoods(3100); //退货}}</strong></span>
(5)运行test2.java,结果如下:
- 使用注解配置spring aop
- 使用注解配置Spring AOP
- 【Spring五】AOP之使用注解配置
- 使用注解配置spring中的aop
- Spring学习之使用注解配置AOP
- Spring AOP注解配置
- Spring AOP 注解配置
- Spring---AOP(注解配置)
- spring aop注解配置
- Spring AOP 注解配置
- spring-aop注解配置
- 【Spring】基于注解的Spring AOP的配置和使用
- 基于spring注解方式配置和使用spring AOP
- Spring 注解方式配置aop:
- spring AOP 零注解配置
- 注解方式配置spring-aop
- Spring基于注解配置AOP
- Spring AOP Aspectj 注解配置
- python数据结构学习笔记-2016-10-21-01-复杂度分析
- POJ1054-The Troublesome Frog
- HTML基础01
- Python之自动单元测试之一(unittest使用实例)
- 进程间通信 - 匿名管道实现
- 使用注解配置Spring AOP
- 水经注软件下载的高程数据如何导出DEM格式
- 洛谷 P1081 开车旅行
- easyui-datagrid合并列 C#使用Dynamic泛型计算合并列数据
- php+html5实现文件断点续传
- Shell数组:shell数组的定义、数组长度
- scala获取当前时间戳和今天0时时间戳(精确到秒)的小工具
- 自适应按钮
- Git远程操作详解