java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available. 方案一報錯:
@Service
public class B implements BInterface {
@Override
public void funTemp() {
// 希望調用本類方法 但是它拋出異常,希望也能夠回滾事務
BInterface b = BInterface.class.cast(AopContext.currentProxy());
System.out.println(b);
b.funB();
}
@Override
public void funB() {
System.out.println(1 / 0);
}
}
方案二報錯:
@Service
public class B implements BInterface {
@Override
public void funTemp() {
System.out.println("Thread A "+Thread.currentThread().getName());
// 希望調用本類方法 但是它拋出異常,希望也能夠回滾事務,
// 希望調用本類方法 但是希望它去異步執行~
BInterface b = BInterface.class.cast(AopContext.currentProxy());
System.out.println(b);
b.funB();
}
@Async
@Override
public void funB() {
System.out.println("Thread B "+Thread.currentThread().getName());
// System.out.println(1 / 0);
}
解決方案:
@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy = true)
public class PayCenterApplicaton {
// https://blog.csdn.net/f641385712/article/details/93475774
public static void main(String[] args) {
SpringApplication.run(PayCenterApplicaton.class,args);
}
}
加上這一段代碼
@EnableAspectJAutoProxy(exposeProxy = true)
參考文章: https://blog.csdn.net/f641385712/article/details/93475774