Spring学习-21:Spring的AOP:基于AspectJ的注解开发

来源:互联网 发布:js鼠标事件怎么用 编辑:程序博客网 时间:2024/05/17 11:08
lAspectJ是一个基于Java语言的AOP框架
lSpring2.0以后新增了对AspectJ切点表达式支持
l@AspectJAspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
l新版本Spring框架,建议使用AspectJ方式来开发AOP
l使用AspectJ需要导入SpringAOPAspectJ相关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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://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 * */@Aspectpublic 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.xsdhttp://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; @Testpublic 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 * */@Aspectpublic 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; @Testpublic 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 * */@Aspectpublic 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 * */@Aspectpublic 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:支持多个切点和多个通知的组合,真实开发中常用。



0 0
原创粉丝点击