注解創建一個Java文件,使用@Aspect注解修飾該類創建一個方法,使用@Before、@After、@Around等進行修飾,在注解中寫上切入點的表達式說明:上述Java文件創建好后,需要將其在Spring的容器中進行聲明,可以在配置文件中定義節點,也可以使用@Componen" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Spring3.0中的AOP注解配置

系統 2042 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條評論
主站蜘蛛池模板: 普洱| 通山县| 聂荣县| 五河县| 大宁县| 黑河市| 东安县| 民权县| 克什克腾旗| 靖安县| 晴隆县| 龙门县| 兴义市| 犍为县| 海城市| 阿拉善盟| 滁州市| 郓城县| 汝城县| 株洲市| 南岸区| 阿鲁科尔沁旗| 巴南区| 梅河口市| 明溪县| 龙游县| 磐安县| 宁蒗| 成都市| 咸宁市| 上虞市| 鱼台县| 华蓥市| 富民县| 伊金霍洛旗| 叙永县| 龙山县| 新建县| 江西省| 绥化市| 辽阳市|