@AspectJ可使用切點函數定義切點,咱們還可使用邏輯運算符對切點進行復核運算獲得複合的切點,爲了在切面中重用切點,咱們還能夠對切點進行命名,以便在其餘的地方引用定義過的切點。當一個鏈接點匹配多個切點時,須要考慮織入順序的問題,此外一個重要的問題是如何再加強中訪問鏈接點上下文的信息。java
package com.yyq.aspectJAdvanced; public interface Waiter { void greetTo(String name); void serveTo(String name); }
NaiveWaiter實現類:spring
package com.yyq.aspectJAdvanced; public class NaiveWaiter implements Waiter { @Override public void greetTo(String name) { System.out.println("NaiveWaiter:greet to " + name + "..."); } @Override public void serveTo(String name) { System.out.println("NaiveWaiter:serving to " + name + "..."); } public void smile(String clientName,int times){ System.out.println("NaiveWaiter:smile to "+clientName+ times+"times..."); } }
NaughtyWaiter實現類:ide
package com.yyq.aspectJAdvanced; public class NaughtyWaiter implements Waiter { public void greetTo(String clientName) { System.out.println("NaughtyWaiter:greet to " + clientName + "..."); } public void serveTo(String clientName) { System.out.println("NaughtyWaiter:serving " + clientName + "..."); } public void joke(String clientName, int times) { System.out.println("NaughtyWaiter:play " + times + " jokes to " + clientName + "..."); } }
Seller接口:函數
package com.yyq.aspectJAdvanced; public interface Seller { int sell(String goods, String clientName); }
SmallSeller實現類:測試
package com.yyq.aspectJAdvanced; public class SmartSeller implements Seller { public int sell(String goods,String clientName) { System.out.println("SmartSeller: sell "+goods +" to "+clientName+"..."); return 100; } public void checkBill(int billId){ if(billId == 1) throw new IllegalArgumentException("iae Exception"); else throw new RuntimeException("re Exception"); } }
beans.xml配置文件:this
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="naiveWaiter" class="com.yyq.aspectJAdvanced.NaiveWaiter"/> <bean id="naughtyWaiter" class="com.yyq.aspectJAdvanced.NaughtyWaiter"/> <bean id="seller" class="com.yyq.aspectJAdvanced.SmartSeller"/> <!-- <bean class="com.yyq.aspectJAdvanced.TestAspect"/> <bean class="com.yyq.aspectJAdvanced.TestAspect2"/> <bean class="com.yyq.aspectJAdvanced.TestAspect3"/> <bean class="com.yyq.aspectJAdvanced.TestAspect4"/> <bean class="com.yyq.aspectJAdvanced.TestAspect5"/> <bean id="naiveWaiter2" class="com.yyq.aspectJAdvanced.NaiveWaiter2"/> <bean class="com.yyq.aspectJAdvanced.TestAspect6"/> <bean class="com.yyq.aspectJAdvanced.TestAspect7"/> <bean class="com.yyq.aspectJAdvanced.TestAspect8"/> --> </beans>
package com.yyq.aspectJAdvanced; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class TestAspect { //與非運算 @Before("!target(com.yyq.aspectJAdvanced.NaiveWaiter) && execution(* serveTo(..))") public void notServeInNaiveWaiter(){ System.out.println("--notServeInNaiveWaiter() executed!--"); } //與運算 @After("within(com.yyq.aspectJAdvanced.*) && execution(* greetTo(..))") public void greetToFun(){ System.out.println("--greetToFun() executed!--"); } //或運算 @AfterReturning("target(com.yyq.aspectJAdvanced.Waiter) || target(com.yyq.aspectJAdvanced.Seller)") public void waiterOrSeller(){ System.out.println("--waiterOrSeller() executed!--"); } }
測試方法:spa
@Test public void pointAspectJTest() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter"); Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter"); naiveWaiter.greetTo("John"); naiveWaiter.serveTo("John"); naughtyWaiter.greetTo("Tom"); naughtyWaiter.serveTo("Tom"); }
package com.yyq.aspectJAdvanced; import org.aspectj.lang.annotation.Pointcut; public class TestNamePointcut { //經過註解方法inPackage()對該切點進行命名,方法可視域修飾符爲private,代表該命名切點只能在本切面類中使用 @Pointcut("within(com.yyq.aspectJAdvaned.*)") private void inPackage(){} @Pointcut("execution(* greetTo(..))") protected void greetTo(){} @Pointcut("inPackage() and greetTo()") public void inPkgGreetTo(){} }
TestAspect2:切面實現類代理
package com.yyq.aspectJAdvanced; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class TestAspect2 { @Before("TestNamePointcut.inPkgGreetTo()") public void pkgGreetTo(){ System.out.println("--pkgGreetTo() executed!--"); } @Before("target(com.yyq.aspectJAdvanced.NaiveWaiter) || TestNamePointcut.inPkgGreetTo()") public void pkgGreetToNotnaiveWaiter(){ System.out.println("--pkgGreetToNotnaiveWaiter() executed!--"); } }
測試方法:code
@Test public void pointAspectJTest2() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean("naiveWaiter"); naiveWaiter.smile("Andy", 2); }
@Aspect public class TestAspect3 { @Around("execution(* greetTo(..)) && target(com.yyq.aspectJAdvanced.NaiveWaiter)") public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable { System.out.println("---joinPointAccess---"); System.out.println("args[0]:" + pjp.getArgs()[0]); System.out.println("signature:" + pjp.getTarget().getClass()); pjp.proceed(); System.out.println("---joinPointAccess---"); } }
測試方法:xml
@Test public void pointAspectJTest3() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter"); naiveWaiter.greetTo("Andy"); }
@Aspect public class TestAspect4 { @Before("target(com.yyq.aspectJAdvanced.NaiveWaiter) && args(name,num,..)") public void bindJoinPointParams(int num, String name) { System.out.println("---bindJoinPointParams---"); System.out.println("name:" + name); System.out.println("num:" + num); System.out.println("---bindJoinPointParams---"); } }
測試方法:
@Test public void pointAspectJTest4() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean("naiveWaiter"); naiveWaiter.smile("Andy", 3); }
@Aspect public class TestAspect5 { @Before("this(waiter)") public void bindProxyObj(Waiter waiter){ System.out.println("---bindProxyObj---"); System.out.println(waiter.getClass().getName()); System.out.println("---bindProxyObj---"); } }
測試方法:
@Test public void pointAspectJTest5() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter waiter = (Waiter) ctx.getBean("naiveWaiter"); waiter.greetTo("Yang"); }
@Aspect public class TestAspect6 { @Before("@within(m)") public void bindTypeAnnoObject(Monitorable m) { System.out.println("---bindTypeAnnoObject---"); System.out.println(m.getClass().getName()); System.out.println("---bindTypeAnnoObject---"); } }
測試方法:
@Test public void pointAspectJTest6() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter waiter = (Waiter) ctx.getBean("naiveWaiter2"); ((NaiveWaiter2)waiter).greetTo("Yang"); }
@Aspect public class TestAspect7 { @AfterReturning(value = "target(com.yyq.aspectJAdvanced.SmartSeller)", returning = "retVal") public void bindReturnValue(int retVal) { System.out.println("---bindReturnValue---"); System.out.println("returnValue:" + retVal); System.out.println("---bindReturnValue---"); } }
測試方法:
@Test public void pointAspectJTest7() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); SmartSeller seller = (SmartSeller) ctx.getBean("seller"); seller.sell("Beer", "John"); }
@Aspect public class TestAspect8 { @AfterThrowing(value = "target(com.yyq.aspectJAdvanced.SmartSeller)", throwing = "iae") public void bindException(IllegalArgumentException iae) { System.out.println("---bindException---"); System.out.println("exception:" + iae.getMessage()); System.out.println("---bindException---"); } }
測試方法:
@Test public void pointAspectJTest8() { String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml"; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); SmartSeller seller = (SmartSeller) ctx.getBean("seller"); seller.checkBill(1); }