国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
知識(shí)點(diǎn)七:Spring AOP技術(shù)
Spring提供了兩種切面使用方式,實(shí)際工作中我們可以選用其中一種:
1 基于xml配置方式進(jìn)行AOP開發(fā)
2 基于注解方式進(jìn)行AOP開發(fā)


下面是基于注解的方式
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"     
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <aop:aspectj-autoproxy/><!-- 啟動(dòng)對(duì)@AspectJ注解的支持 -->
</beans>



Java代碼
  1. //@Aspect 表示此類是一個(gè)切面,包含切入點(diǎn)和通知   
  2. //@Component 表示將此切面交由spring容器管理,此時(shí)必須在配置文件里面開啟自動(dòng)掃描   
  3. //  開啟自動(dòng)掃描   <context:component-scan base-package="cn.itcast"/>   
  4. @Aspect  @Component  
  5. public class MyInterceptor {   
  6. //(1) 切入點(diǎn)的描述   
  7. /**  
  8.      *@Pointcut :表示規(guī)定切入點(diǎn)   
  9.      *execution() 語法規(guī)范  
  10.      * 第一個(gè)“*”表示任意返回結(jié)果類型  
  11.      * “cn.itcast.service.impl.PersonServiceBean”:表示對(duì)此類進(jìn)行攔截,  
  12.      * 如果是cn.itcast.service..*.*:表示對(duì)包c(diǎn)n.itcast.service以及子包里所  
  13. 有的類的所有方法進(jìn)行攔截,  
  14.      * (..)表示參數(shù)   
  15.      */    
  16. @Pointcut("execution(*    
  17. cn.itcast.service.impl.PersonServiceBean.*(..))")   
  18. //(2)聲明一個(gè)切入點(diǎn)   
  19. public void anyMethod(){}   
  20.        
  21. //(3) 聲明通知及注入方式   
  22. @Before("anyMethod()")   
  23. public void doAccessCheck(){   
  24.         System.out.println("前置通知");   
  25.     }   
  26. @AfterReturning("anyMethod()")   
  27.     public void doAfterReturning(){   
  28.         System.out.println("后置通知");   
  29.     }   
  30.     @After("anyMethod()")   
  31.     public void doAfter(){   
  32.         System.out.println("最終通知");   
  33.     }   
  34.     @AfterThrowing("anyMethod()")   
  35.     public void doAfterThrowing(){   
  36.         System.out.println("異常通知");   
  37.     }   
  38.     @Around("anyMethod()"//環(huán)繞通知的方法簽名是規(guī)定的?。。?  
  39.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{   
  40.         //if(){適合判斷用戶權(quán)限   
  41.         System.out.println("進(jìn)入方法");   
  42.         Object result = pjp.proceed();   
  43.         System.out.println("退出方法");   
  44.         //}   
  45.         return result;   
  46.     }      
  47. }  


注冊(cè)給容器后,如果執(zhí)行此切面規(guī)定攔截的類的方法時(shí),按照@Before等方式加上“通知”

得到的運(yùn)行結(jié)果!?。。。∫欢ㄗ⒁忭樞?
前置通知
進(jìn)入方法
我是save()方法
后置通知
最終通知
退出方法
―――――――――――
如果想在通知中得到攔截方法的參數(shù),該怎么做?
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name){
System.out.println("前置通知");
}
注意:通知方法的參數(shù)名一定要和args指定的一致?。。?

如果想得到攔截方法的返回結(jié)果,該怎么做?


Java代碼
  1. @AfterReturning(pointcut="anyMethod()",returning="result")   
  2. public void doAfterReturning(String result){   
  3.         System.out.println("后置通知:"+result);   
  4. }  



如果想得到攔截方法拋出的異常,該怎么做?


Java代碼
  1. @AfterThrowing(pointcut="anyMethod()",throwing="e")   
  2. public void doAfterThrowing(Exception e){   
  3.         System.out.println("異常通知:"+e);   
  4. }  


基于xml配置方式進(jìn)行AOP開發(fā)

<aop:config>
    <aop:aspect id="asp" ref="myInterceptor">
            <aop:pointcut id="mycut"
expression="execution(* cn.itcast.service..*.*(..))"/>
            <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
            <aop:after pointcut-ref="mycut" method="doAfterReturning"/>
            <aop:after-returning pointcut-ref="mycut" method="doAfter"/>
            <aop:after-throwing
pointcut-ref="mycut" method="doAfterThrowing"/>
            <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
          </aop:aspect>
    </aop:config>

如果只是攔截返回值為String類型的方法,切面可以這樣寫
<aop:pointcut id="mycut" expression="execution(java.lang.String
cn.itcast.service..*.*(..))"/>
如果要求第一個(gè)參數(shù)為String類型,不管其后面有幾個(gè)參數(shù)
<aop:pointcut id="mycut" expression="execution(*
cn.itcast.service..*.*(java.lang.String,..))"/>
如果要求返回值類型不為void,怎么寫?           
<aop:pointcut id="mycut" expression="execution(!void
cn.itcast.service..*.*(..))"/>
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
SpringAop
Spring AOP進(jìn)行日志記錄,管理
spring 技術(shù)詳解 教程(三)
Spring AOP示例(注解方式)
Spring AOP 的@Aspect (轉(zhuǎn))
Spring-AOP核心
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服