Spring学习-21:Spring的AOP:基于AspectJ的注解开发
2017-04-13 13:44
936 查看
lAspectJ是一个基于Java语言的AOP框架
lSpring2.0以后新增了对AspectJ切点表达式支持
l@AspectJ是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
l新版本Spring框架,建议使用AspectJ方式来开发AOP
l使用AspectJ需要导入SpringAOP和AspectJ相关jar包:
1、spring-aop-3.2.0.RELEASE.jar
2、com.springsource.org.aopalliance-1.0.0.jar
3、spring-aspects-3.2.0.RELEASE.jar
4、com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
通过配置启用@AspectJ切面:
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
AspectJ是一个基于Java语言的AOP框架
Spring2.0以后新增了对AspectJ切点表达式支持
@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
新版本Spring框架,建议使用AspectJ方式来开发AOP
AspectJ表达式:
* 语法:execution(表达式)
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
* execution(“* cn.itcast.spring3.demo1.dao.*(..)”) ---只检索当前包
* execution(“* cn.itcast.spring3.demo1.dao..*(..)”) ---检索包及当前包的子包.
* execution(* cn.itcast.dao.GenericDAO+.*(..)) ---检索GenericDAO及子类
AspectJ增强:
@Before 前置通知,相当于BeforeAdvice
@AfterReturning 后置通知,相当于AfterReturningAdvice
@Around 环绕通知,相当于MethodInterceptor
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
@DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)
下面详细介绍详细的使用方法(基于注解、基于XML),并做举例说明。稍后介绍基于xml配置的方式来使用AspectJ。
1、基于注解的AspectJ开发
第一步:引入相应jar包(如上所述)
第二步:编写被增强的类,即目标类
第三步:编写切面类,即切点和增强的结合
第四步:在applicationContext.xml中引入约束,配置bean,并开启自动代理
第五步:编写测试类
第六步:运行测试,查看结果。
实现了对指定的目标类中的指定方法的增强。
AspectJ提供了6种通知的类型,我们重点介绍前面5种。
@Before前置通知,相当于BeforeAdvice
* 就在方法之前执行.没有办法阻止目标方法执行的.
@AfterReturning后置通知,相当于AfterReturningAdvice
* 后置通知,获得方法返回值.
@Around环绕通知,相当于MethodInterceptor
* 在可以方法之前和之后来执行的,而且可以阻止目标方法的执行.
@AfterThrowing抛出通知,相当于ThrowAdvice
@After最终final通知,不管是否异常,该通知都会执行
@DeclareParents引介通知,相当于IntroductionInterceptor (不要求掌握)
第一种前置通知我们上面的例子中已经使用过。
修改MyAspect类,来试试afterReturning
在后置增强种获得方法的返回值:
修改目标类:
修改测试类:
运行测试:
下面举个例子使用剩下的几种通知:
修改切面类:
修改目标类:
运行测试类:
【注】最后讲解一个小知识点:基于AspectJ的切点定义。
关于切点的注解,切点是我们的增强最重要应用的方法。为什么要定义一个切点呢?因为我们上述开发中,很多通知的value表达式都是重复的,在实际开发中,每写一个通知,就要去写一个表达式,很繁琐。所以我们可以采用定义切点的方式来解决。
如何定义呢?请看例子:
修改MyAspect类:定义一个切点
如何使用这个切点呢?
可以发现,相较于没有定义切点的注解配置方式,配置切点的方式来使用注解,要方便很多。
【拓展】
Advisor和Aspect的区别:
Advisor:Spring传统意义上的切面,支持一个切点和一个通知的组合
Aspect:支持多个切点和多个通知的组合,真实开发中常用。
lSpring2.0以后新增了对AspectJ切点表达式支持
l@AspectJ是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
l新版本Spring框架,建议使用AspectJ方式来开发AOP
l使用AspectJ需要导入SpringAOP和AspectJ相关jar包:
1、spring-aop-3.2.0.RELEASE.jar
2、com.springsource.org.aopalliance-1.0.0.jar
3、spring-aspects-3.2.0.RELEASE.jar
4、com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
通过配置启用@AspectJ切面:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 开启AspectJ自动代理--> <aop:aspectj-autoproxy /> </beans>
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
AspectJ是一个基于Java语言的AOP框架
Spring2.0以后新增了对AspectJ切点表达式支持
@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
新版本Spring框架,建议使用AspectJ方式来开发AOP
AspectJ表达式:
* 语法:execution(表达式)
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
* execution(“* cn.itcast.spring3.demo1.dao.*(..)”) ---只检索当前包
* execution(“* cn.itcast.spring3.demo1.dao..*(..)”) ---检索包及当前包的子包.
* execution(* cn.itcast.dao.GenericDAO+.*(..)) ---检索GenericDAO及子类
AspectJ增强:
@Before 前置通知,相当于BeforeAdvice
@AfterReturning 后置通知,相当于AfterReturningAdvice
@Around 环绕通知,相当于MethodInterceptor
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
@DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)
下面详细介绍详细的使用方法(基于注解、基于XML),并做举例说明。稍后介绍基于xml配置的方式来使用AspectJ。
1、基于注解的AspectJ开发
第一步:引入相应jar包(如上所述)
第二步:编写被增强的类,即目标类
package com.js.demo1; /** * 目标类 * @author JiangShuai * */ public class UserDao { public void add(){ System.out.println("add..."); } public void update(){ System.out.println("update..."); } public void delete(){ System.out.println("delete..."); } public void find(){ System.out.println("find..."); } }
第三步:编写切面类,即切点和增强的结合
package com.js.demo1; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 切面类:就是切点与增强的结合 * @Aspect:用于定义切面 * @Before:用于定义前置增强,只使用其value属性时,value可以省略,其值为表达式 * 表示你想对哪些类进行增强,可精确到某包、某类、某方法,控制灵活 * @author JiangShuai * */ @Aspect public class MyAspect { @Before(value="execution(* com.js.demo1.UserDao.add(...))") public void before(){ System.out.println("前置增强==="); } }
第四步:在applicationContext.xml中引入约束,配置bean,并开启自动代理
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 自动生成代理,底层就是AnnotationAwareAspectJAutoProxyCreator --> <aop:aspectj-autoproxy/> <bean id="userDao" class="com.js.demo1.UserDao"> </bean> <bean id="myAspect" class="com.js.demo1.MyAspect"></bean> </beans>
第五步:编写测试类
package com.js.demo1; /** * 测试类 */ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestDemo { @Autowired @Qualifier("userDao") private UserDao userDao; @Test public void demo1(){ userDao.add(); userDao.delete(); userDao.find(); userDao.update(); } }
第六步:运行测试,查看结果。
实现了对指定的目标类中的指定方法的增强。
AspectJ提供了6种通知的类型,我们重点介绍前面5种。
@Before前置通知,相当于BeforeAdvice
* 就在方法之前执行.没有办法阻止目标方法执行的.
@AfterReturning后置通知,相当于AfterReturningAdvice
* 后置通知,获得方法返回值.
@Around环绕通知,相当于MethodInterceptor
* 在可以方法之前和之后来执行的,而且可以阻止目标方法的执行.
@AfterThrowing抛出通知,相当于ThrowAdvice
@After最终final通知,不管是否异常,该通知都会执行
@DeclareParents引介通知,相当于IntroductionInterceptor (不要求掌握)
第一种前置通知我们上面的例子中已经使用过。
修改MyAspect类,来试试afterReturning
package com.js.demo1; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 切面类:就是切点与增强的结合 * @Aspect:用于定义切面 * @Before:用于定义前置增强,只使用其value属性时,value可以省略,其值为表达式 * 表示你想对哪些类进行增强,可精确到某包、某类、某方法,控制灵活 * @author JiangShuai * */ @Aspect public class MyAspect { @Before(value="execution(* com.js.demo1.UserDao.add(..))") public void before(){ System.out.println("前置增强==="); } @AfterReturning(value="execution(* com.js.demo1.UserDao.update(..))") public void afterReturnin(){ System.out.println("后置增强==="); } }
在后置增强种获得方法的返回值:
修改目标类:
package com.js.demo1; /** * 目标类 * @author JiangShuai * */ public class UserDao { public void add(){ System.out.println("add..."); } public int update(){ System.out.println("update..."); return 1; } public void delete(){ System.out.println("delete..."); } public void find(){ System.out.println("find..."); } }
修改测试类:
package com.js.demo1; /** * 测试类 */ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestDemo { @Autowired @Qualifier("userDao") private UserDao userDao; @Test public void demo1(){ userDao.add(); userDao.delete(); userDao.find(); userDao.update(); } }
运行测试:
下面举个例子使用剩下的几种通知:
修改切面类:
package com.js.demo1; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 切面类:就是切点与增强的结合 * @Aspect:用于定义切面 * @Before:用于定义前置增强,只使用其value属性时,value可以省略,其值为表达式 * 表示你想对哪些类进行增强,可精确到某包、某类、某方法,控制灵活 * @author JiangShuai * */ @Aspect public class MyAspect { @Before(value="execution(* com.js.demo1.UserDao.add(..))") public void before(){ System.out.println("前置增强==="); } @AfterReturning(value="execution(* com.js.demo1.UserDao.update(..))",returning="resultVal") public void afterReturnin(JoinPoint joinPoint,Object resultVal){ System.out.println("后置增强===方法的返回值:"+resultVal); } @Around(value="execution(* com.js.demo1.UserDao.update(..))") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("环绕前增强==="); Object object=proceedingJoinPoint.proceed(); //调用目标方法 System.out.println("环绕后增强==="); return object; } @AfterThrowing(value="execution(* com.js.demo1.UserDao.find(..))",throwing="e") public void afterThrowing(Throwable e){ System.out.println("不好了,出异常了==="+e.getMessage()); } @After(value="execution(* com.js.demo1.UserDao.find(..))") public void after(){ System.out.println("最终通知==="); } }
修改目标类:
package com.js.demo1; /** * 目标类 * @author JiangShuai * */ public class UserDao { public void add(){ System.out.println("add..."); } public int update(){ System.out.println("update..."); return 1; } public void delete(){ System.out.println("delete..."); } public void find(){ System.out.println("find..."); int d=1/0; //抛出异常,测试@afterThrowing } }
运行测试类:
【注】最后讲解一个小知识点:基于AspectJ的切点定义。
关于切点的注解,切点是我们的增强最重要应用的方法。为什么要定义一个切点呢?因为我们上述开发中,很多通知的value表达式都是重复的,在实际开发中,每写一个通知,就要去写一个表达式,很繁琐。所以我们可以采用定义切点的方式来解决。
如何定义呢?请看例子:
修改MyAspect类:定义一个切点
@Pointcut("execution(* com.js.demo1.UserDao.find(..))") private void myPointcut(){ }
如何使用这个切点呢?
package com.js.demo1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
* 切面类:就是切点与增强的结合
* @Aspect:用于定义切面
* @Before:用于定义前置增强,只使用其value属性时,value可以省略,其值为表达式
* 表示你想对哪些类进行增强,可精确到某包、某类、某方法,控制灵活
* @author JiangShuai
*
*/
@Aspect
public class MyAspect {
@Before(value="execution(* com.js.demo1.UserDao.add(..))")
public void before(){
System.out.println("前置增强===");
}
@AfterReturning(value="execution(* com.js.demo1.UserDao.update(..))",returning="resultVal")
public void afterReturnin(JoinPoint joinPoint,Object resultVal){
System.out.println("后置增强===方法的返回值:"+resultVal);
}
@Around(value="execution(* com.js.demo1.UserDao.update(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕前增强===");
Object object=proceedingJoinPoint.proceed(); //调用目标方法
System.out.println("环绕后增强===");
return object;
}
@AfterThrowing(value="MyAspect.myPointcut()",throwing="e")
public void afterThrowing(Throwable e){
System.out.println("不好了,出异常了==="+e.getMessage());
}
@After(value="MyAspect.myPointcut()")
public void after(){
System.out.println("最终通知===");
}
@Pointcut("execution(* com.js.demo1.UserDao.find(..))") private void myPointcut(){ }
}
可以发现,相较于没有定义切点的注解配置方式,配置切点的方式来使用注解,要方便很多。
【拓展】
Advisor和Aspect的区别:
Advisor:Spring传统意义上的切面,支持一个切点和一个通知的组合
Aspect:支持多个切点和多个通知的组合,真实开发中常用。
相关文章推荐
- Spring Aop开发基于AspectJ注解方式的案例
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- spring框架的学习(五)——Spring使用AspectJ进行AOP的开发:注解方式
- Spring(十二)AspectJ框架开发AOP(基于注解)
- spring-redis缓存方案学习三:基于aop的自定义注解开发
- 学习《spring 3.x企业应用开发实战》之基于@AspectJ和Schema的AOP
- Web框架梳理:第四章:Spring学习入门、Spring属性注入、AOP编程、注解开发
- Spring 【Aspectj】进行AOP开发 (【注解】方式)
- 10、SSM框架-Spring AOP之基于注解的声明式AspectJ(10)
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- Spring AOP使用配置介绍(四):基于@AspectJ注解的aop
- Spring学习记录(十二)---AOP理解和基于注解配置
- Spring的AOP的AspectJ的开发:(注解)
- spring-aop学习【基于注解】
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
- Spring学习总结6(AOP-基于注解)
- Spring学习笔记四(AOP中的通知参数和注解开发)
- Spring4学习笔记-AOP(基于注解的方式)
- Spring入门学习初体验(2)----基于注解的AOP
- Spring4学习笔记-AOP(基于注解的方式)