<context:component-scan base-package="com.zztaiwll.service,com.zztaiwll.util.annotion">         <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/> </context:component-scan>  4,本工程使用的maven,所以引入兩個(gè)相關(guān)的包,分別是aspectjweaver.jar和aspectjrt.jar    <spring.version>4.0.2.RELEASE</spring.version>        <dependency>            <groupId>aspectj</groupId>            <artifactId>aspectjrt</artifactId>            <version>1.5.4</version>        </dependency>    </dependencies>    <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>${spring.version}</version>        </dependency>5,定義切入點(diǎn)(1)所謂的定義切入點(diǎn),實(shí)質(zhì)就是為一個(gè)切入點(diǎn)表達(dá)式起一個(gè)名稱,從而容許在多個(gè)增強(qiáng)處理中重用該名稱(2)切入點(diǎn)定義包含兩個(gè)部分:    [1]一個(gè)切入點(diǎn)表達(dá)式    [2]一個(gè)包含名字和任意參數(shù)的方法簽名例如:@Aspectpublic class demo {@Pointcut("execution(* com.zztaiwll.controller.*.*(..))")public void myPointcut(){};}@Aspectpublic class demoDesc {//直接使用demo切面類的myPointcut切入點(diǎn)    @AfterReturning(returning="rtc",pointcut="demo.myPointcut()")    //聲明 rtc時(shí)指定類型會(huì)相知目標(biāo)方法必須返回指定類型的值或者您沒(méi)有返回值    public void log(Object rtc){         System.out.println("獲取該函數(shù)返回至"+rvt);    }}6,切入點(diǎn)指示符Spring AOP一共支持如下幾種切入點(diǎn)指示符【1】,execution:用于匹配方法的連接點(diǎn),是springaop的最主要的切入點(diǎn)指示符execution(modifiters-pattern?ret-type-pattern declaring-type-pattern ?name-pattern(param-pattern) throw-pattern)modifiters-pattern:指定方法的修飾符,支持通配符,該部分可以省略ret-type-pattern:指定方法的返回值類型,支持通配符,可以使用*通配符來(lái)匹配所有返回值類型declaring-type-pattern:指定方法所屬的類,支持通配符,可省略name-pattern:指定匹配的方法名,支持通配符,可使用*指定所有方法param-pattern:指定方法中的形參列表,支持兩個(gè)通配符,即“*”和“..",其中*代表一個(gè)人以參數(shù),..代表另個(gè)或者多個(gè)任意類型的參數(shù)throw-pattern:制定方法聲明拋出的異常,支持通配符,可省略如execution(* com.zztaiwll.controller.*.*(..))指的是controller下所有的類中的所有方法【2】,within:用于限定匹配特定類型的連接點(diǎn),當(dāng)使用Springaop的時(shí)候,智能匹配方法執(zhí)行的連接點(diǎn)如witnin(com.zztaiwll.controller..*)【3】,this用于限定AOP代理必須是指定類型的實(shí)例,匹配該對(duì)象的所有連接點(diǎn)。當(dāng)使用Spring AOP的時(shí)候,只能匹配方法的連接點(diǎn)this(com.zztaiwll.controller.UserController)【4】,target:用于限定目標(biāo)對(duì)象必須是指定的類型的實(shí)例,匹配該對(duì)象所有的連接點(diǎn),當(dāng)使用Spring AOP的時(shí)候,只能匹配方法的連接點(diǎn)target(com.zztaiwll.controller.UserController)【5】,args:用于對(duì)連接點(diǎn)的類型進(jìn)行限制,要求參數(shù)類型是指定類型的實(shí)例。當(dāng)使用Spring AOP的時(shí)候,只能匹配方法的連接點(diǎn)args(java.io.Serializable)【6】,bean:用于限定之匹配指定Bean十里內(nèi)的連接點(diǎn),實(shí)際上只是用方法執(zhí)行作為連接點(diǎn)bean表達(dá)式是需要傳入Bean的id或name,表示只匹配該bean實(shí)例內(nèi)的連接點(diǎn),支持使用*通配符bean(*service)7,織入點(diǎn)表達(dá)式spring支持使用如下三個(gè)邏輯運(yùn)算符來(lái)組合切入點(diǎn)表達(dá)式【1】&&:要求連接點(diǎn)同時(shí)匹配兩個(gè)切入點(diǎn)表達(dá)式【2】||:滿足任意一個(gè)即可【3】!要求連接點(diǎn)不匹配指定的切入點(diǎn)表達(dá)式如  @Around(value="execution(* com.zztaiwll.controller.*.*(..))&& @annotation(json)")8,編寫(xiě)相應(yīng)的方法{1}Before,After,AfterThrowing,AfterReturning@Component//聲明這是一個(gè)切面Bean@Aspectpublic class ServiceAspect {    private final static Log log = LogFactory.getLog(ServiceAspect.class);    //配置切入點(diǎn),該方法無(wú)方法體,主要為方便同類中其他方法使用此處配置的切入點(diǎn)    @Before("execution(* com.zztaiwll.controller.*.*(..))")    public void aspect(JoinPoint jp) throws Throwable{            Object []obj=jp.getArgs();//獲取參數(shù)        System.out.println("1111111111111111111111");    }    //(1),pointcut/value:這兩個(gè)屬性是一樣的,他們都是用于指定該切入點(diǎn)對(duì)應(yīng)的切入表達(dá)式    //(2),returning:該屬性制定一個(gè)形參名,用于表示Advice方法中可定義與此同名的形參,該形參用于訪問(wèn)目標(biāo)方法的返回值,他可以用于限定切入點(diǎn)之匹配具體對(duì)應(yīng)返回值的方法    @AfterReturning(returning="rvt", pointcut="execution(* com.zztaiwll.controller.*.*(..))")    public Object after(Object rvt){        System.out.println("獲取該函數(shù)返回至"+rvt);        return rvt;    }    //(1),pointcut/value:這兩個(gè)屬性是一樣的,他們都是用于指定該切入點(diǎn)對(duì)應(yīng)的切入表達(dá)式    //(2),throwing:該屬性制定一個(gè)形參名,用于表示Advice方法中可定義與此同名的形參,該形參可用于訪問(wèn)目標(biāo)方法拋出的異常    @AfterThrowing(throwing="ex",pointcut="execution(* com.zztaiwll.controller.*.*(..))")    public void throwError(Throwable ex){        System.out.println("目標(biāo)方法拋出異常"+ex);        System.out.println("模擬Advice對(duì)異常的修復(fù)");    }    //AfterReturning和After的區(qū)別    //(1),AfterReturning增強(qiáng)處理只有在目標(biāo)方法完全成功后才被織入    //(2),After增強(qiáng)處理不管目標(biāo)方法如何結(jié)束,他都會(huì)被織入    @After("execution(* com.zztaiwll.controller.*.*(..))")    public void afterS(JoinPoint jp){        Object []obj=jp.getArgs();//獲取參數(shù)        System.out.println("模擬方法釋放資源");    }}{2},Around@Around注解用于修飾Around增強(qiáng)處理,它的功能比較強(qiáng)大,他近似等于Before增強(qiáng)處理和AfterReturn增強(qiáng)處理的總和,Around增強(qiáng)處理即可以在執(zhí)行目標(biāo)方法之前織入增強(qiáng)動(dòng)作也可在執(zhí)行目標(biāo)方法之后織入增強(qiáng)動(dòng)作//聲明這是一個(gè)組件@Component//聲明這是一個(gè)切面Bean@Aspectpublic class ServiceAspect {    private final static Log log = LogFactory.getLog(ServiceAspect.class);    //配置切入點(diǎn),該方法無(wú)方法體,主要為方便同類中其他方法使用此處配置的切入點(diǎn)    @Around(value="execution(* com.zztaiwll.controller.*.*(..))")    public Object aspect(ProceedingJoinPoint pjd, json json) throws Throwable{            Object[] args=pjd.getArgs();//獲取參數(shù)        Object rvt=pjd.proceed();//獲取返回結(jié)果        return rvt;    }}第二部分,做自定義注解,注意關(guān)鍵一點(diǎn)要在spring-servlet.xml中配上<context:component-scan base-package="com.zztaiwll.service,com.zztaiwll.util.annotion">         <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>    </context:component-scan>【1】,配置自定義注解import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)//@Target({ ElementType.METHOD })//public @interface json {}【2】aop實(shí)現(xiàn)注解//聲明這是一個(gè)組件@Component//聲明這是一個(gè)切面Bean@Aspectpublic class ServiceAspect {    private final static Log log = LogFactory.getLog(ServiceAspect.class);    //配置切入點(diǎn),該方法無(wú)方法體,主要為方便同類中其他方法使用此處配置的切入點(diǎn)    @Around(value="execution(* com.zztaiwll.controller.*.*(..))&& @annotation(json)")    public Object aspect(ProceedingJoinPoint pjd, json json) throws Throwable{            Object[] args=pjd.getArgs();        Object rvt=pjd.proceed();        Object objs=StringConvertJSON.toJSON(rvt);        return objs;    }}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注