javascript
Spring-AOP @AspectJ切点函数之@within()和@target
文章目錄
- 概述
- @target(M)的匹配規則
- @within(M)的匹配規則
- 實例
- @target
- @within
- 注意事項
概述
除了@annotation和@args外,還有另外兩個用于注解的切點函數,分別是@target和@within.
和@annotation @args函數一樣,@target和@within也只接受注解類名作為入參。
其中@target(M)匹配任意標注了@M的目標類,而@within(M)匹配標注了@M的類及其子孫類(子類經測試匹配不到,歡迎指正)
@target(M)的匹配規則
@target使用@target(注解類型全限定名)匹配當前目標對象類型的執行方法, 必須是在目標對象上聲明注解,在接口上聲明不起作用
@within(M)的匹配規則
經驗證,目前發現和 @target(M)的匹配規則是一樣的。
@within(注解類型全限定名)匹配所有持有指定注解的類里面的方法, 即要把注解加在類上. 在接口上聲明不起作用 。 子孫類經測試匹配不到,如有錯誤煩請指出。
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
@target
首先自定義一個注解用于測試用
package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** * * @ClassName: Mark* * @Description: 自定義注解* * @author: Mr.Yang* * @date: 2017年9月5日 下午12:02:46*/@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface Mark {public String value() default "";}4個注解標注的POJO ,繼承關系 C3 -->C2–> C1–> C0
package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import org.springframework.stereotype.Component;/*** * * @ClassName: C0* * @Description: @Component 標注的Bean* * @author: Mr.Yang* * @date: 2017年9月5日 下午1:36:37*/@Component public class C0 {public void methodName() {System.out.println("C0 method executed");} }C1類聲明處標注了自定義注解@Mark
package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import org.springframework.stereotype.Component;@Mark @Component public class C1 extends C0 {public void methodName() {System.out.println("C1 method executed");} }C2類聲明處標注了自定義注解@Mark
package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import org.springframework.stereotype.Component;@Mark @Component public class C2 extends C1 {public void methodName() {System.out.println("C2 method executed");} } package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import org.springframework.stereotype.Component;@Component public class C3 extends C2 {public void methodName() {System.out.println("C3 method executed");} }使用@Aspect標注的環繞增強切面
package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: AtTargetAspect* * @Description: 標注了@Aspect的切面 環繞增強切面* * @author: Mr.Yang* * @date: 2017年9月5日 上午11:59:26*/@Aspect public class AtTargetAspect {@Around("@target(com.xgj.aop.spring.advisor.aspectJ.function.attarget.Mark)")public void crossCuttingCode(ProceedingJoinPoint joinPoint)throws Throwable {System.out.println("****AtTargetAspect.crossCuttingCode() : "+ joinPoint.getSignature().getName()+ ": Before Method Execution");try {joinPoint.proceed();} finally {// Do Something useful, If you have}System.out.println("****AtTargetAspect.crossCuttingCode() : "+ joinPoint.getSignature().getName()+ ": After Method Execution");} }配置文件
<?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)掃描類包以及應用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.attarget"/><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.attarget.AtTargetAspect"/></beans>測試代碼
package com.xgj.aop.spring.advisor.aspectJ.function.attarget;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class AtTargetAspectTest {private ApplicationContext ctx;@Testpublic void test() {ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJ/function/attarget/conf-attarget.xml");C0 c0 = ctx.getBean("c0", C0.class);C1 c1 = ctx.getBean("c1", C1.class);C2 c2 = ctx.getBean("c2", C2.class);C3 c3 = ctx.getBean("c3", C3.class);// C0沒有標注了@Mark,不會被織入增強c0.methodName();// C1標注了@Mark,會被織入增強c1.methodName();// C2標注了@Mark,會被織入增強c2.methodName();// C3沒有標注了@Mark,不會被織入增強c3.methodName();} }運行結果:
2017-09-05 13:40:56,244 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c24cb3: startup date [Tue Sep 05 13:40:56 BOT 2017]; root of context hierarchy 2017-09-05 13:40:56,338 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/attarget/conf-attarget.xml] C0 method executed ****AtTargetAspect.crossCuttingCode() : methodName: Before Method Execution C1 method executed ****AtTargetAspect.crossCuttingCode() : methodName: After Method Execution ****AtTargetAspect.crossCuttingCode() : methodName: Before Method Execution C2 method executed ****AtTargetAspect.crossCuttingCode() : methodName: After Method Execution C3 method executedC1 和 C2標注了@Mark注解,可以看到成功的織入了@Around環繞增強
@within
自定義一個注解
package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** * * @ClassName: Mark* * @Description: 自定義注解* * @author: Mr.Yang* * @date: 2017年9月5日 下午12:02:46*/@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface Mark2 {public String value() default "";}4個注解聲明的POJO Bean
package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import org.springframework.stereotype.Component;/*** * * @ClassName: C0* * @Description: @Component 標注的Bean* * @author: Mr.Yang* * @date: 2017年9月5日 下午1:36:37*/@Component public class A0 {public void methodName() {System.out.println("A0 method executed \n");} } package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import org.springframework.stereotype.Component;/*** * * @ClassName: A1* * @Description: 標注了@Mark2,可以被增強* * @author: Mr.Yang* * @date: 2017年9月5日 下午6:02:21*/@Mark2 @Component public class A1 extends A0 {public void methodName() {System.out.println("A1 method executed");} } package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import org.springframework.stereotype.Component;@Component public class A2 extends A1 {public void methodName() {System.out.println("A2 method executed");} } package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import org.springframework.stereotype.Component;@Component public class A3 extends A2 {public void methodName() {System.out.println("A3 method executed");} }切面 實現了Ordered接口
package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.core.Ordered;/*** * * @ClassName: AtWithinAspect* * @Description: 標注了@Aspect 環繞增強切面* * @author: Mr.Yang* * @date: 2017年9月5日 下午1:53:40*/@Aspect public class AtWithinAspect implements Ordered {@Around("@within(com.xgj.aop.spring.advisor.aspectJ.function.atwithin.Mark2)")public void crossCuttingCode(ProceedingJoinPoint joinPoint)throws Throwable {System.out.println("****AtWithinAspect.crossCuttingCode() : "+ joinPoint.getSignature().getName()+ ": Before Method Execution");try {joinPoint.proceed();} finally {// Do Something useful, If you have}System.out.println("****AtWithinAspect.crossCuttingCode() : "+ joinPoint.getSignature().getName()+ ": After Method Execution \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)掃描類包以及應用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJ.function.atwithin"/><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.atwithin.AtWithinAspect"/></beans>測試類
package com.xgj.aop.spring.advisor.aspectJ.function.atwithin;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class AtWithinAspectTest {private ApplicationContext ctx;@Testpublic void test() {ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/aop/spring/advisor/aspectJ/function/atwithin/conf-atwithin.xml");A0 a0 = ctx.getBean("a0", A0.class);A1 a1 = ctx.getBean("a1", A1.class);A2 a2 = ctx.getBean("a2", A2.class);A3 a3 = ctx.getBean("a3", A3.class);// A0沒有標注@Mark,不會被織入增強a0.methodName();// A1標注了@Mark,會被織入增強a1.methodName();// A2沒有標注@Mark,不會被織入增強a2.methodName();// A3沒有標注@Mark,不會被織入增強a3.methodName();} }運行結果:
2017-09-05 18:04:01,685 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c24cb3: startup date [Tue Sep 05 18:04:01 BOT 2017]; root of context hierarchy 2017-09-05 18:04:01,793 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/atwithin/conf-atwithin.xml] A0 method executed ****AtWithinAspect.crossCuttingCode() : methodName: Before Method Execution A1 method executed ****AtWithinAspect.crossCuttingCode() : methodName: After Method Execution A2 method executed A3 method executed注意事項
如果標注了@M注解的是一個接口,則所有實現該接口的類并不匹配@within(M) . 假設接口Waiter標注了@Mark注解,但是它的實現類NaiveWaiter、NaughtyWaiter這些接口實現類滅有標注@Mark, 則@within(com.xgj.Mark) 和 @target(com,xgj.Mark)都不匹配NaiveWaiter、NaughtyWaiter。 因為@within() @target() @annotation函數都是針對目標類而言的,而非針對運行時的引用類型而言。 需要特別注意。
驗證過程見github中的 com.xgj.aop.spring.advisor.aspectJ.function.atwithin2
輸出結果:
2017-09-05 18:11:18,171 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24b9371e: startup date [Tue Sep 05 18:11:18 BOT 2017]; root of context hierarchy 2017-09-05 18:11:18,285 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/atwithin2/beans.xml] NaughtyWaiter:greet to XiaoGong... NaiveWaiter:greet to Jiang...標注在接口上的@@Monitorable,使用within ,實現類中的方法并沒有匹配
總結
以上是生活随笔為你收集整理的Spring-AOP @AspectJ切点函数之@within()和@target的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring-AOP @AspectJ切
- 下一篇: Spring-AOP @AspectJ切