spring註解方式設置exposeProxy=true

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章