javascript
aop阻止方法运行_Spring AOP无法拦截内部方法调用
假設(shè)一個(gè)接口里面有兩個(gè)方法:
package demo.long;public interfaceCustomerService {public voiddoSomething1();public voiddoSomething2();
}
接口實(shí)現(xiàn)類如下:
package demo.long.impl;import demo.long.CustomerService;public class CustomerServiceImpl implementsCustomerService {public voiddoSomething1() {
System.out.println("CustomerServiceImpl.doSomething1()");
doSomething2();
}public voiddoSomething2() {
System.out.println("CustomerServiceImpl.doSomething2()");
}
}
現(xiàn)在我需要在CustomerService接口的每個(gè)方法被調(diào)用時(shí)都在方法前執(zhí)行一些邏輯,所以需要配置一個(gè)攔截器:
package demo.long;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;
@Aspectpublic classCustomerServiceInterceptor {
@Before("execution(* demo.long..*.*(..))")public voiddoBefore() {
System.out.println("do some important things before...");
}
}
把Bean加到Spring配置中
如果現(xiàn)在外部對象調(diào)用CustomerService的doSomething1()方法的時(shí)候,會發(fā)現(xiàn)只有doSomething1()方法執(zhí)行前打印了“do some important things before...”,而doSomething1()內(nèi)部調(diào)用doSomething2()時(shí)并沒有打印上述內(nèi)容;外部對象單獨(dú)調(diào)用doSomething2()時(shí)會打印上述內(nèi)容。
public classCustomerServiceTest {
@Autowired
ICustomerService customerService;
@Testpublic voidtestAOP() {
customerService.doSomething1();
}
}
原因分析
攔截器的實(shí)現(xiàn)原理就是動態(tài)代理,實(shí)現(xiàn)AOP機(jī)制。Spring 的代理實(shí)現(xiàn)有兩種:一是基于 JDK Dynamic Proxy 技術(shù)而實(shí)現(xiàn)的;二是基于 CGLIB 技術(shù)而實(shí)現(xiàn)的。如果目標(biāo)對象實(shí)現(xiàn)了接口,在默認(rèn)情況下Spring會采用JDK的動態(tài)代理實(shí)現(xiàn)AOP,CustomerServerImpl正是這種情況。
JDK動態(tài)代理生成的CustomerServiceImpl的代理類大致如下:
public class CustomerServiceProxy implementsCustomerService {privateCustomerService customerService;public voidsetCustomerService(CustomerService customerService) {this.customerService =customerService;
}public voiddoSomething1() {
doBefore();
customerService.doSomething1();
}public voiddoSomething2() {
doBefore();
customerService.doSomething2();
}private voiddoBefore() {//例如,可以在此處開啟事務(wù)或記錄日志
System.out.println("do some important things before...");
}
}
客戶端程序使用代理類對象去調(diào)用業(yè)務(wù)邏輯:
public classTestProxy {public static voidmain(String[] args) {//創(chuàng)建代理目標(biāo)對象//對于Spring來說,這一工作是由Spring容器完成的。
CustomerService serviceProxyTarget = newCustomerServiceImpl();//創(chuàng)建代理對象//對于Spring來說,這一工作也是由Spring容器完成的。
CustomerServiceProxy serviceProxy = newCustomerServiceProxy();
serviceProxy.setCustomerService(serviceProxyTarget);
CustomerService serviceBean=(CustomerService) serviceProxy;//調(diào)用業(yè)務(wù)邏輯操作
serviceBean.doSomething1();
}
}
執(zhí)行main方法,發(fā)現(xiàn)doSomething1()中調(diào)用doSomething2()方法的時(shí)候并未去執(zhí)行CustomerServiceProxy類的doBefore()方法。其實(shí)doSomething2()等同于this.doSomething2(),在CustomerServiceImpl類中this關(guān)鍵字表示的是當(dāng)前這個(gè)CustomerServiceImpl類的實(shí)例,所以程序會去執(zhí)行CustomerServiceImpl對象中的doSomething2()方法,而不會去執(zhí)行CustomerServiceProxy類對象中的 doSomething2()方法。
在使用Spring AOP的時(shí)候,我們從IOC容器中獲取的Bean對象其實(shí)都是代理對象,而不是那些Bean對象本身,由于this關(guān)鍵字引用的并不是該Service Bean對象的代理對象,而是其本身,因此Spring AOP是不能攔截到這些被嵌套調(diào)用的方法的。
解決方案
修改類,不要出現(xiàn)“自調(diào)用”的情況:這是Spring文檔中推薦的“最佳”方案;
若一定要使用“自調(diào)用”,那么this.doSomething2()替換為:((CustomerService) AopContext.currentProxy()).doSomething2();此時(shí)需要修改spring的aop配置:
總結(jié)
以上是生活随笔為你收集整理的aop阻止方法运行_Spring AOP无法拦截内部方法调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西游乐园军人优待证免费吗?
- 下一篇: 项目如何用jetty运行_阿里大牛教你如