Spring3.0中的AOP注解配置

系统 1574 0

转自: http://zywang.iteye.com/blog/974226

使用@AspectJ标签

  1. 在配置文件中添加 <aop:aspectj-autoproxy/> 注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

示例:

Java代码 收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;
  2. import org.aspectj.lang.annotation.After;
  3. import org.aspectj.lang.annotation.AfterThrowing;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Before;
  7. import org.springframework.stereotype.Component;
  8. /**
  9. *基于注解的AOP日志示例
  10. *@authorZYWANG2011-3-24
  11. */
  12. @Component
  13. @Aspect
  14. public class AopLog{
  15. //方法执行前调用
  16. @Before ( "execution(*com.zywang.services.impl.*.*(..))" )
  17. public void before(){
  18. System.out.println( "before" );
  19. }
  20. //方法执行后调用
  21. @After ( "execution(*com.zywang.services.impl.*.*(..))" )
  22. public void after(){
  23. System.out.println( "after" );
  24. }
  25. //方法执行的前后调用
  26. @Around ( "execution(*com.zywang.services.impl.*.*(..))" )
  27. public Objectaround(ProceedingJoinPointpoint) throws Throwable{
  28. System.out.println( "beginaround" );
  29. Objectobject=point.proceed();
  30. System.out.println( "endaround" );
  31. return object;
  32. }
  33. //方法运行出现异常时调用
  34. @AfterThrowing (pointcut= "execution(*com.zywang.services.impl.*.*(..))" ,throwing= "ex" )
  35. public void afterThrowing(Exceptionex){
  36. System.out.println( "afterThrowing" );
  37. System.out.println(ex);
  38. }
  39. }

上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

Java代码 收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;
  2. import org.aspectj.lang.annotation.Around;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. *基于注解的AOP日志示例
  9. *@authorZYWANG2011-3-24
  10. */
  11. @Component
  12. @Aspect
  13. public class AopLog{
  14. @Pointcut ( "execution(*com.iflysse.school.services.impl.*.*(..))" )
  15. public void pointcut(){}
  16. //方法执行前调用
  17. @Before ( "pointcut()" )
  18. public void before(){
  19. System.out.println( "before" );
  20. }
  21. //方法执行的前后调用
  22. @Around ( "pointcut()" )
  23. public Objectaround(ProceedingJoinPointpoint) throws Throwable{
  24. System.out.println( "beginaround" );
  25. Objectobject=point.proceed();
  26. System.out.println( "endaround" );
  27. return object;
  28. }
  29. }


Spring3.0中的AOP注解配置


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论