戴着假发的程序员出品 抖音ID:戴着假发的程序员 欢迎关注
EnableAspectJAutoProxy的exposeProxy属性
spring应用手册(第三部分)
前面我们以及解释完了AOP的所有配置和使用方式。
现在我们来看看下面的案例:
我们准备一个业务类:在业务类中我们有两个方法showMessage和formartMsg。我们再showMessage中调用formartMsg方法:
/**
* @author 戴着假发的程序员
*
* @description
*/
@Component
public class MessageService {
public String showMessage(String info) {
System.out.println("OtherInfoServcie-showMessage展示信息:"+info);
this.formartMsg(info);
return null;
}
public String formartMsg(String info){
System.out.println("OtherInfoServcie-formartMsg对象消息"+info+"进行格式化");
return info;
}
}
添加一个Aspect类,在其中增加一个前置通知:
/**
* @author 戴着假发的程序员
*
* @description
*/
@Component
@Aspect
public class DKAspect1 {
@Pointcut("this(com.st.dk.demo8.service.MessageService)")
public void pointcut1(){}
@Before("pointcut1()")
public void before(JoinPoint joinPoint){
System.out.println("前置通知,被增强的方法是:"+joinPoint.toString());
}
}
测试:
ApplicationContext ac =
new AnnotationConfigApplicationContext(AppConfig.class);
MessageService bean = ac.getBean(MessageService.class);
bean.showMessage("戴着假发的程序员");
结果:
这是我们会发现,shwoMessage方法被拦截了,但是formartMsg方法并没有被拦截。
这是什么原因,其实spring官方已经给了明确的解释,解释的内容有点繁杂,我就不在这里截图。
我简答解释一下,就是我们使用AOP,必然会生成一个MessageService的代理对象。所以我们调用showMessage方法就是调用了代理对象的showMessage方法,必然会被增强,但是在shwoMessage中使用的this并非代理对象,而是我们的原生对象,所以this.formartMsg必然不会被增强。
如果解决呢?
方案1:
我们在我们的业务类中注入一个自己本身,然后把this替换为注入的对象,就可以解决问题。
这种方式很显然,非常的不优雅。
所以spring 提供了其他的解决方案。
方案二:
修改EnableAspectJAutoProxy的属性exposeProxy为true,这是我们的代理对象接口会被暴漏在ThreadLocal中,我们就可以直接获取了。
@EnableAspectJAutoProxy(exposeProxy=true)
使用 方式:
/**
* @author 戴着假发的程序员
*
* @description
*/
@Component
public class MessageService {
public String showMessage(String info) {
System.out.println("OtherInfoServcie-showMessage展示信息:"+info);
//使用AopContext的静态方法获取当前的代理对象
((MessageService)AopContext.currentProxy()).formartMsg(info);
return null;
}
public String formartMsg(String info){
System.out.println("OtherInfoServcie-formartMsg对象消息"+info+"进行格式化");
return info;
}
}
在测试: