javascript
Spring-AOP @AspectJ切点函数之within()
- 概述
- 語法
- 實例
- withincomxgjNaiveWaiter
- withincomxgj
- withincomxgj
- withincomxgjMark
概述
通過類匹配模式串聲明切點,within()函數定義的連接點是針對目標類而言的,而非針對運行期對象的類型而言,這一點和execution()是相同的。
但是within()和execution()函數不同的是,within()所指定的連接點最小范圍只能是類,而execution()所指定的連接點可以大到包,小到方法入參。 所以從某種意義上講,execution()函數功能涵蓋了within()函數的功能
語法
within(<類匹配模式>)比如 within(com.xgj.NaiveWaiter),是within()函數能表達的最小粒度。
用法舉例:
within(com.xgj.NaiveWaiter) 匹配目標類NaiveWaiter的所有方法。 如果切點調整為within(com.xgj.Waiter),則NaiveWaiter和NaughtyWaiter中的所有方法都不匹配。 而Waiter本身是接口,不可能實例化,所以within(com.xgj.Waiter)的聲明是無意義的。
within(com.xgj.*) 匹配com.xgj包中的所有類的方法,但是不包含子孫包中類的方法。
within(com.xgj..*)匹配com.xgj包以及子孫包中的所有類的方法都匹配這個切點
within(@com.xgj.Mark *) 匹配com.xgj及子包下帶有@com.xgj.Mark 注解的任何類(接口不行)的任何方法
實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
within(com.xgj.NaiveWaiter)
接口類
package com.xgj.aop.spring.advisor.aspectJ.function.within;public interface Waiter {void greetTo(String clientName);void serverTo(String clientName); }、
注解標注的2個POJO
package com.xgj.aop.spring.advisor.aspectJ.function.within;import org.springframework.stereotype.Component;/*** * * @ClassName: NaughtyWaiter* * @Description: @Component標注的bean* * @author: Mr.Yang* * @date: 2017年9月5日 上午1:31:10*/@Component public class NaughtyWaiter implements Waiter {@Overridepublic void greetTo(String clientName) {System.out.println("NaughtyWaiter greetTo " + clientName);}@Overridepublic void serverTo(String clientName) {System.out.println("NaughtyWaiter greetTo " + clientName);}} package com.xgj.aop.spring.advisor.aspectJ.function.within;import org.springframework.stereotype.Component;/*** * * @ClassName: NaiveWaiter* * @Description: @Component標注的Bean* * @author: Mr.Yang* * @date: 2017年9月5日 上午1:30:52*/@Component public class NaiveWaiter implements Waiter {@Overridepublic void greetTo(String clientName) {System.out.println("NaiveWaiter greetTo " + clientName);}@Overridepublic void serverTo(String clientName) {System.out.println("NaiveWaiter serverTo " + clientName);}}增強切面
package com.xgj.aop.spring.advisor.aspectJ.function.within;import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect;/*** * * @ClassName: WithinAspect* * @Description: 標注了@Aspect的切面* * @author: Mr.Yang* * @date: 2017年9月5日 上午1:21:17*/@Aspect public class WithinAspect {@AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within.NaiveWaiter)")public void crossCuttingCode() {System.out.println("后置增強 some logic is here\n");}}配置文件
<?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.within"/><!-- 基于@AspectJ切面的驅動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJ.function.within.WithinAspect"/></beans>測試類
package com.xgj.aop.spring.advisor.aspectJ.function.within;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class WithinAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml");NaiveWaiter naiveWaiter = ctx.getBean("naiveWaiter", NaiveWaiter.class);naiveWaiter.greetTo("XiaoGongJiang");naiveWaiter.serverTo("XiaoGongJiang");NaughtyWaiter naughtyWaiter = ctx.getBean("naughtyWaiter",NaughtyWaiter.class);naughtyWaiter.greetTo("XiaoGongJiang");naughtyWaiter.serverTo("XiaoGongJiang");} }運行結果:
2017-09-05 01:32:21,687 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@24b9371e: startup date [Tue Sep 05 01:32:21 BOT 2017]; root of context hierarchy 2017-09-05 01:32:21,786 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml] NaiveWaiter greetTo XiaoGongJiang 后置增強 some logic is hereNaiveWaiter serverTo XiaoGongJiang 后置增強 some logic is hereNaughtyWaiter greetTo XiaoGongJiang NaughtyWaiter greetTo XiaoGongJiang可以看到,只有NaiveWaiter類被織入了橫切邏輯。
如果我們將切面中的within函數改為within(com.xgj.aop.spring.advisor.aspectJ.function.within.Waiter)
@Aspect public class WithinAspect {@AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within.Waiter)")public void crossCuttingCode() {System.out.println("后置增強 some logic is here\n");}}再此運行
2017-09-05 01:33:27,989 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@61ee30d2: startup date [Tue Sep 05 01:33:27 BOT 2017]; root of context hierarchy 2017-09-05 01:33:28,066 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml] NaiveWaiter greetTo XiaoGongJiang NaiveWaiter serverTo XiaoGongJiang NaughtyWaiter greetTo XiaoGongJiang NaughtyWaiter greetTo XiaoGongJiangwithin(com.xgj.*)
先增加一個子目錄seller, 然后 改造下 切面類
@Aspect public class WithinAspect {// 匹配com.xgj.aop.spring.advisor.aspectJ.function.within包下的所有類的所有方法,不包括子孫包@AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within.*)")public void crossCuttingCode() {System.out.println("后置增強 some logic is here\n");} }測試類獲取SmartSeller,然后調用目標方法
package com.xgj.aop.spring.advisor.aspectJ.function.within;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.xgj.aop.spring.advisor.aspectJ.function.within.seller.SmartSeller;public class WithinAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml");NaiveWaiter naiveWaiter = ctx.getBean("naiveWaiter", NaiveWaiter.class);naiveWaiter.greetTo("XiaoGongJiang");naiveWaiter.serverTo("XiaoGongJiang");NaughtyWaiter naughtyWaiter = ctx.getBean("naughtyWaiter",NaughtyWaiter.class);naughtyWaiter.greetTo("XiaoGongJiang");naughtyWaiter.serverTo("XiaoGongJiang");SmartSeller smartSeller = ctx.getBean("smartSeller", SmartSeller.class);smartSeller.smileTo("XiaoGongJiang");smartSeller.jokeTo("XiaoGongJiang");} }運行結果:
2017-09-05 01:39:44,352 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a06514b: startup date [Tue Sep 05 01:39:44 BOT 2017]; root of context hierarchy 2017-09-05 01:39:44,433 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml] NaiveWaiter greetTo XiaoGongJiang 后置增強 some logic is hereNaiveWaiter serverTo XiaoGongJiang 后置增強 some logic is hereNaughtyWaiter greetTo XiaoGongJiang 后置增強 some logic is hereNaughtyWaiter greetTo XiaoGongJiang 后置增強 some logic is hereSmartSeller serverTo XiaoGongJiang SmartSeller greetTo XiaoGongJiang可以看到,只有當前目錄下的所有類的方法被織入了橫切邏輯,而子孫包中的沒有被織入增強。
within(com.xgj..*)
改造下切面類
@Aspect public class WithinAspect {// 匹配com.xgj.aop.spring.advisor.aspectJ.function.within包下的所有類的所有方法,包括子孫包@AfterReturning("within(com.xgj.aop.spring.advisor.aspectJ.function.within..*)")public void crossCuttingCode() {System.out.println("后置增強 some logic is here\n");} }再此運行
2017-09-05 01:42:56,488 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a06514b: startup date [Tue Sep 05 01:42:56 BOT 2017]; root of context hierarchy 2017-09-05 01:42:56,599 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml] NaiveWaiter greetTo XiaoGongJiang 后置增強 some logic is hereNaiveWaiter serverTo XiaoGongJiang 后置增強 some logic is hereNaughtyWaiter greetTo XiaoGongJiang 后置增強 some logic is hereNaughtyWaiter greetTo XiaoGongJiang 后置增強 some logic is hereSmartSeller serverTo XiaoGongJiang 后置增強 some logic is hereSmartSeller greetTo XiaoGongJiang 后置增強 some logic is here可以看到,子孫包中的所有類的所有方法都能被織入了增強.
within(@com.xgj.Mark *)
增加一個自定義的注解,或者使用框架自帶的注解 都可以,用于測試
package com.xgj.aop.spring.advisor.aspectJ.function.within;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: Mart* * @Description: 自定義注解* * @author: Mr.Yang* * @date: 2017年9月5日 下午12:02:46*/@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented public @interface Mart {public String value() default "";}NaiveWaiter類標注@Mart
package com.xgj.aop.spring.advisor.aspectJ.function.within;import org.springframework.stereotype.Component;/*** * * @ClassName: NaiveWaiter* * @Description: @Component標注的Bean* * @author: Mr.Yang* * @date: 2017年9月5日 上午1:30:52*/@Mart @Component public class NaiveWaiter implements Waiter {@Overridepublic void greetTo(String clientName) {System.out.println("NaiveWaiter greetTo " + clientName);}@Overridepublic void serverTo(String clientName) {System.out.println("NaiveWaiter serverTo " + clientName);}}NaughtyWaiter沒有類標注@Mart
子目錄 SmartSeller 標注 @Mart
package com.xgj.aop.spring.advisor.aspectJ.function.within.seller;import org.springframework.stereotype.Component;import com.xgj.aop.spring.advisor.aspectJ.function.within.Mart;/*** * * @ClassName: SmartSeller* * @Description: @Component標注的Bean* * @author: Mr.Yang* * @date: 2017年9月5日 上午1:30:52*/@Mart @Component public class SmartSeller {public void jokeTo(String clientName) {System.out.println("SmartSeller greetTo " + clientName);}public void smileTo(String clientName) {System.out.println("SmartSeller serverTo " + clientName);}}修改切面
@AfterReturning("within(@com.xgj.aop.spring.advisor.aspectJ.function.within.Mart *)")public void crossCuttingCode() {System.out.println("后置增強 some logic is here\n");}運行測試類
2017-09-05 17:50:54,071 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@54397c28: startup date [Tue Sep 05 17:50:54 BOT 2017]; root of context hierarchy 2017-09-05 17:50:54,179 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJ/function/within/conf-within.xml] NaiveWaiter greetTo XiaoGongJiang 后置增強 some logic is hereNaiveWaiter serverTo XiaoGongJiang 后置增強 some logic is hereNaughtyWaiter greetTo XiaoGongJiang NaughtyWaiter greetTo XiaoGongJiang SmartSeller serverTo XiaoGongJiang 后置增強 some logic is hereSmartSeller greetTo XiaoGongJiang 后置增強 some logic is here匹配com.xgj.aop.spring.advisor.aspectJ.function.within及子包下帶有@com.xgj.aop.spring.advisor.aspectJ.function.within.Mark 注解的任何類(接口不行)的任何方法
總結
以上是生活随笔為你收集整理的Spring-AOP @AspectJ切点函数之within()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java-Java5.0泛型解读
- 下一篇: Spring-AOP @AspectJ切