※XML設定
applicationContext.xml
<aop:config> <aop:pointcut expression="execution(public void aop.AOP.A())" id="aaa" /> <aop:pointcut expression="execution(public void aop.AOP.B(String))" id="bbb" /> <aop:pointcut expression="execution(public String aop.AOP.C())" id="ccc" /> <aop:pointcut expression="execution(public String aop.AOP.D(String))" id="ddd" /> <aop:aspect ref="beforeAfter"> <!-- <aop:after-returning method="after" pointcut-ref="aaa" /> --> <!-- <aop:after-returning method="after" pointcut-ref="bbb" /> --> <!-- <aop:after-returning method="after" pointcut-ref="ccc" /> --> <!-- <aop:after-returning method="after" pointcut-ref="ddd" /> --> <!-- <aop:after-throwing method="after" pointcut-ref="aaa" /> --> <!-- <aop:after-throwing method="after" pointcut-ref="bbb" /> --> <!-- <aop:after-throwing method="after" pointcut-ref="ccc" /> --> <!-- <aop:after-throwing method="after" pointcut-ref="ddd" /> --> <aop:around method="after" pointcut-ref="aaa" /> <aop:around method="after" pointcut-ref="bbb" /> <aop:around method="after" pointcut-ref="ccc" /> <aop:around method="after" pointcut-ref="ddd" /> </aop:aspect> </aop:config>
※整理成一張表
before/after | 程式碼正常 | throws | try/catch | 無throws和try/catch |
會執行AOP | ✔ | ✔ | ✔ | ✔ |
會執行Advice | ✔ | ✔ | ✔ | ✔ |
繼續往下執行 | ✔ | ✔ | ✔ | |
after-returning | 程式碼正常 | throws | try/catch | 無throws和try/catch |
會執行AOP | ✔ | ✔ | ||
會執行Advice | ✔ | ✔ | ✔ | ✔ |
繼續往下執行 | ✔ | ✔ | ✔ | |
after-throwing | 程式碼正常 | throws | try/catch | 無throws和try/catch |
會執行AOP | ✔ | ✔ | ||
會執行Advice | ✔ | ✔ | ✔ | ✔ |
繼續往下執行 | ✔ | ✔ | ✔ | |
around | 程式碼正常 | throws | try/catch | 無throws和try/catch |
會執行AOP | ✔ | ✔ | ✔ | ✔ |
會執行Advice | ||||
繼續往下執行 | ✔ | ✔ | ✔ | ✔ |
※Annotation設定
因為不要藕合,所以AOP.java不知道BeforeAfter.java的存在,所以只要在BeforeAfter增加即可applicationContext.xml
<context:component-scan base-package="\" /> <aop:aspectj-autoproxy/> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
※第2和第3行擇其一即可
@Named @Aspect public class BeforeAfter { @Before("execution(public * aop.AOP.*(..))") public void before() { System.out.println("之前執行!"); } @After("execution(public * aop.AOP.*(..))") public void after() { System.out.println("之後執行!"); } }
@Named @Aspect public class BeforeAfter { @Pointcut("execution(public * aop.AOP.*(..))") private void shareAOP(){} @Before("BeforeAfter.shareAOP()") public void before() { System.out.println("之前執行!"); } @After("BeforeAfter.shareAOP()") public void after() { System.out.println("之後執行!"); } }
※這個等同xml的pointcut-ref 功能,一定要有「()」
@Named @Aspect public class BeforeAfter { @Pointcut("execution(public * aop.AOP.*(..))") private void shareAOP(){} @AfterReturning(pointcut = "BeforeAfter.shareAOP()") // @AfterReturning("execution(public * aop.AOP.*(..))") public void returnAfter() { System.out.println("return 之後執行!"); } @AfterThrowing(pointcut = "BeforeAfter.shareAOP()") // @AfterThrowing("execution(public * aop.AOP.*(..))") public void throwAfter() { System.out.println("throw執行!"); } @Around("BeforeAfter.shareAOP()") // @Around("execution(public * aop.AOP.*(..))") public void after() { System.out.println("around執行!"); } }
※@AfterReturning、@AfterThrowing、@Around用法都一樣
※如果只要匹配A和C方法,可以這樣下
@Pointcut("execution(public * aop.AOP.A(..)) || execution(public * aop.AOP.C(..))")
沒有留言:
張貼留言