--- title: Spring @Transactional, @Async 失效時不推薦autowired自己的方式 --- # Spring @Transactional, @Async 失效時不推薦autowired自己的方式 ###### tags: `JAVA` ## 前因 同一個class 調用時,物件並不是從sp容器獲取,無法經由sp aop觸發sp有關事務等功能。 常見解決方法是autowired自己的類, 也就是建立一個selfAgent() 並且return自己類,然後調用selfAgent以後調用裡面的方法 這可以解決大多數的情況,但... ## 狀況 工作中遇到的情況是,在重構一個段落,而裡面有調用selfAgent時,我開啟多線程以後,新線程運行到調用selfAgent行即沒有繼續下去了,真tmd難除錯 ## 解決 將原本 調用selfAgent以獲取sp托管的自己類 的方式,改用SpringContextUtil.getBean() 獲取自己類 ``` SpringContextUtil.getBean(this.getClass()).targetMethod(); ``` https://blog.csdn.net/hellozhxy/article/details/109753711
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up