
概述
众所周知,当SpringBoot中一个类下有a、b两个方法时,如果该类内的a方法直接调用b方法,则b方法上基于代理实现的注解会无法生效。典型地有:@Transactional、@Async、@Retryable注解。例如下述例子
@Service
public class DogService {
public void sayHi(){
// 此时,调用sayBye方法,则@Async注解不会生效
sayBye();
}
@Async
public void sayBye() {
...
}
}
解决方案
方法拆分到不同类中
简单粗暴地办法,是将同类中的两个方法拆分到两个类当中
@Service
public class DogService1 {
@Autowired
private DogService2 dogService2;
public void sayHi(){
dogService2.sayBye();
}
}
...
@Service
public class DogService2 {
@Async
public void sayBye() {
...
}
}
自我注入
在该类中进行自我注入,然后通过注入后的实例来进行调用。如下所示
@Service
public class DogService3 {
@Autowired
private DogService3 dogService3;
public void sayHi(){
dogService3.sayBye();
}
@Async
public void sayBye() {
...
}
}
此举在低版本SpringBoot版本中可能会提示依赖循环,可在配置文件application.properties中添加下述配置
# 使能依赖循环
spring.main.allow-circular-references=true
获取代理对象
之所以同类下方法调用会导致注解失效,是因为此时没有走代理进行调用。既然如此,那么解决思路就是获取类的代理对象,然后通过该代理进行方法调用。首先在启动类上添加 @EnableAspectJAutoProxy 注解。注意需要将exposeProxy属性设置为true,以实现暴露代理对象
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@SpringBootApplication
public class SpringBoot1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot1Application.class, args);
}
}
然后在业务代码中即可通过AopContext获取代理对象
@Service
public class DogService4 {
public void sayHi(){
// 获取代理对象
DogService4 thisServiceProxy = (DogService4)AopContext.currentProxy();
// 通过代理对象进行方法调用
thisServiceProxy.sayBye();
}
@Async
public void sayBye() {
...
}
}
原文始发于微信公众号(青灯抽丝):Spring中基于代理实现的注解未生效解决方案
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/261040.html