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

黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

Spring3.0中的AOP注解配置

系統(tǒng) 2295 0

轉(zhuǎn)自: http://zywang.iteye.com/blog/974226

使用@AspectJ標(biāo)簽

  1. 在配置文件中添加 <aop:aspectj-autoproxy/> 注解
  2. 創(chuàng)建一個Java文件,使用@Aspect注解修飾該類
  3. 創(chuàng)建一個方法,使用@Before、@After、@Around等進行修飾,在注解中寫上切入點的表達(dá)式

說明:上述Java文件創(chuàng)建好后,需要將其在Spring的容器中進行聲明,可以在配置文件中定義<bean/>節(jié)點,也可以使用@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. //方法執(zhí)行前調(diào)用
  16. @Before ( "execution(*com.zywang.services.impl.*.*(..))" )
  17. public void before(){
  18. System.out.println( "before" );
  19. }
  20. //方法執(zhí)行后調(diào)用
  21. @After ( "execution(*com.zywang.services.impl.*.*(..))" )
  22. public void after(){
  23. System.out.println( "after" );
  24. }
  25. //方法執(zhí)行的前后調(diào)用
  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. //方法運行出現(xiàn)異常時調(diào)用
  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. }

上面這段代碼中多次使用了重復(fù)的切入點,這種情況下,可以使用@Pointcut標(biāo)注,來修改一個切入點方法(這個方法不需要參數(shù)和方法體),然后就可以在@Before等標(biāo)注中引用該方法作為切入點,示例如下:

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. //方法執(zhí)行前調(diào)用
  17. @Before ( "pointcut()" )
  18. public void before(){
  19. System.out.println( "before" );
  20. }
  21. //方法執(zhí)行的前后調(diào)用
  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注解配置


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論