日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring-AOP @AspectJ切点函数之target()和this()

發(fā)布時間:2025/3/21 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-AOP @AspectJ切点函数之target()和this() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 實例
    • target()
    • this()

概述

target()切點函數(shù)通過判斷目標(biāo)類是否按類型匹配指定類來決定連接點是否匹配. 用于匹配當(dāng)前目標(biāo)對象類型的執(zhí)行方法;注意是目標(biāo)對象的類型匹配,這樣就不包括引入接口也類型匹配;

this()切點函數(shù)則通過判斷代理類是否按類型匹配指定類來決定是否和切點匹配。 用于匹配當(dāng)前AOP代理對象類型的執(zhí)行方法;注意是AOP代理對象的類型匹配,這樣就可能包括引入接口也類型匹配。 this中使用的表達式必須是類型全限定名,不支持通配符。

兩者都僅接受類名的入?yún)?#xff0c;雖然類名可以帶“+”,但是對于這兩個函數(shù)來講,使用或者不是用,效果完全相同。


實例

代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster


target()

target(M)表示如果目標(biāo)類按類型匹配于M,這目標(biāo)類的所有方法都匹配切點。

  • target(com.xgj.IBussiness) :IBussiness為接口,匹配接口實現(xiàn)類中所有方法,包括未在接口中聲明的方法
  • target(com.xgj.IBussiness)等同于target(com.xgj.IBussiness+)

接口

package com.xgj.aop.spring.advisor.aspectJ.function.target;public interface IBussinessService {String doSomething();}

目標(biāo)Bean

package com.xgj.aop.spring.advisor.aspectJ.function.target;import org.springframework.stereotype.Component;/*** * * @ClassName: BussinessService* * @Description: @Component標(biāo)注的bean* * @author: Mr.Yang* * @date: 2017年9月5日 下午8:18:03*/@Component public class BussinessService {public String doSomething() {System.out.println("doSomething executed");return "success";} }

切面

package com.xgj.aop.spring.advisor.aspectJ.function.target;import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: TargetAspect* * @Description: @Aspect標(biāo)注的切面* target(com.xgj.aop.spring.advisor.aspectJ.function.* target.Class1)等同于* target(com.xgj.aop.spring.advisor.aspectJ.function* .target.Class1+)* * @author: Mr.Yang* * @date: 2017年9月5日 下午7:53:52*/@Aspect public class TargetAspect {@AfterReturning("target(com.xgj.aop.spring.advisor.aspectJ.function.target.IBussinessService)")public void crossCuttingCode() {System.out.println("some logic is here");} }

配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應(yīng)用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.target"/><!-- 基于@AspectJ切面的驅(qū)動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.target.TargetAspect"/></beans>

測試類

package com.xgj.aop.spring.advisor.aspectJ.function.target;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TargetAspectTest {private ApplicationContext applicationContext;@Testpublic void test() {applicationContext = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJ/function/target/conf-target.xml");BussinessService bussinessService = applicationContext.getBean("bussinessService", BussinessService.class);// 織入增強bussinessService.doSomething();// 織入增強bussinessService.doAnother();} }

運行結(jié)果:

2017-09-05 20:37:52,147 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@285211ef: startup date [Tue Sep 05 20:37:52 BOT 2017]; root of context hierarchy 2017-09-05 20:37:52,264 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/target/conf-target.xml] doSomething executed some logic is here doAnother executed some logic is here

this()

一般情況下,使用this()和target()來匹配定義切點,二者是等效的

  • target(com.xgj.IBussiness)等價于 this(com.xgj.IBussiness)
  • target(com.xgj.BussinessService)等價于this(com.xgj.BussinessService)

二者的區(qū)別體現(xiàn)在通過引介切面產(chǎn)生代理對象時的具體表現(xiàn)。

看個例子:

接口

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;public interface IBussinessService {String doBussiness(); }

實現(xiàn)類

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.springframework.stereotype.Component;/*** * * @ClassName: BussinessService* * @Description: @Component標(biāo)注的bean* * @author: Mr.Yang* * @date: 2017年9月5日 下午8:18:03*/@Component public class BussinessService implements IBussinessService {@Overridepublic String doBussiness() {System.out.println("doBussiness executed");return "success";}public String doAnother() {System.out.println("doAnother executed");return "success";} }

另外一個通過引介切面要實現(xiàn)的接口

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;public interface ITransportService {public void doTransport(); }

實現(xiàn)類

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;public class TransportService implements ITransportService {@Overridepublic void doTransport() {System.out.println("doTransport executed");}}

為Bussiness添加 ITransportService接口的切面

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.DeclareParents; import org.springframework.core.Ordered;/*** * * @ClassName: AddTransportForBussinessAspect* * @Description: 為Bussiness添加 ITransportService接口的切面* * @author: Mr.Yang* * @date: 2017年9月5日 下午9:18:50*/@Aspect public class AddTransportForBussinessAspect implements Ordered {// (1)value 為BussinessService添加接口實現(xiàn), (2)defaultImpl要添加的接口的默認(rèn)的接口實現(xiàn)類@DeclareParents(value = "com.xgj.aop.spring.advisor.aspectJ.function.thisFun.BussinessService", defaultImpl = TransportService.class)public ITransportService iTransportService; // (3) 要實現(xiàn)的目標(biāo)接口@Overridepublic int getOrder() {return 2;}}

橫切邏輯切面

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.core.Ordered;/*** * * @ClassName: ThisAspect* * @Description: @Aspectn標(biāo)注的切面* * * @author: Mr.Yang* * @date: 2017年9月5日 下午8:50:26*/@Aspect public class ThisAspect implements Ordered {// 織入任何運行期對象為ITransportService的Bean中@AfterReturning("this(com.xgj.aop.spring.advisor.aspectJ.function.thisFun.ITransportService)")public void corssCuttingCode() {System.out.println("some logic is here \n ");}@Overridepublic int getOrder() {return 1;} }

配置文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應(yīng)用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.thisFun"/><!-- 基于@AspectJ切面的驅(qū)動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.thisFun.ThisAspect"/> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.thisFun.AddTransportForBussinessAspect"/></beans>

測試類

package com.xgj.aop.spring.advisor.aspectJ.function.thisFun;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class ThisAspectTest {private ApplicationContext applicationContext;@Testpublic void test() {applicationContext = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJ/function/thisFun/conf-this.xml");BussinessService bussinessService = (BussinessService) applicationContext.getBean("bussinessService");// 匹配 thisbussinessService.doBussiness();// 匹配 thisbussinessService.doAnother();// 匹配 this((ITransportService) bussinessService).doTransport();} }

運行結(jié)果

2017-09-05 22:24:03,301 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24177697: startup date [Tue Sep 05 22:24:03 BOT 2017]; root of context hierarchy 2017-09-05 22:24:03,397 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/thisFun/conf-this.xml] doBussiness executed some logic is here doAnother executed some logic is here doTransport executed some logic is here

如果有多個切面,注意多切面織入的順序,如果不加織入的順序, doTransport 方法的切面無法織入。

可見代理對象的方法都織入了this()函數(shù)定義的切面。

總結(jié)

以上是生活随笔為你收集整理的Spring-AOP @AspectJ切点函数之target()和this()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。