@postconstruct注解方法没有执行_被标记为事务的方法互相调用的坑(下)
上一節,主要分析了 被標記為事務的方法互相調用,事務失效的原因,思考比較多,這一節主要說說解決方案,思考會少一些。
解決方案的核心: 通過代理對象去調用方法
1.把方法放到不同的類:
如果想學習Java工程化、高性能及分布式、深入淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給大家。
我們需要新建一個接口:
public interface OtherService {void insertCodeMonkey(); }再定義一個類去實現這個接口:
@Service public class OtherServiceImpl implements OtherService {@AutowiredAccountMapper mapper;@Override@Transactional(propagation=Propagation.REQUIRES_NEW)public void insertCodeMonkey() {Account account = new Account();account.setAccount("CodeMonkey");account.setPassword("CodeMonkey");mapper.insert(account);int a = 1 / 0;} }修改原本的實現類:
@Service public class AccountSerivceImpl implements AccountService {@AutowiredAccountMapper mapper;@AutowiredOtherService otherService;@Transactional@Overridepublic void insertCodeBear() {try {otherService.insertCodeMonkey();} catch (Exception e) {e.printStackTrace();}Account account = new Account();account.setAccount("CodeBear");account.setPassword("CodeBear");mapper.insert(account);} }運行,查看數據庫:
只有一條數據,insertCodeBear方法執行成功了,insertCodeMonkey執行失敗,并且回滾了。
讓我們再看看控制臺的日志:
如果想學習Java工程化、高性能及分布式、深入淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給大家。
可以看到是開了兩個事務去執行的。
這種解決方案最簡單,不需要了解其他東西,但是這種方案需要修改代碼結構,本來兩個方法都是屬于同一個類的,現在需要強行把它們拆開。
2. AopContext:
我們的目標是要在實現類中獲取本類的代理對象,Spring提供了Aop上下文,即:AopContext,通過AopContext,可以很方便的獲取到代理對象:
@Service public class AccountSerivceImpl implements AccountService {@AutowiredAccountMapper mapper;@Transactional@Overridepublic void insertCodeBear() {try {((AccountService)AopContext.currentProxy()).insertCodeMonkey();} catch (Exception ex) {ex.printStackTrace();}Account account = new Account();account.setAccount("CodeBear");account.setPassword("CodeBear");mapper.insert(account);}@Transactional(propagation = Propagation.REQUIRES_NEW)@Overridepublic void insertCodeMonkey() {Account account = new Account();account.setAccount("CodeMonkey");account.setPassword("CodeMonkey");mapper.insert(account);int a = 1 / 0;} }當寫好代碼,很愉快的去測試,發現竟然報錯了:
翻譯下:不能找到當前的代理,需要設置exposeProxy屬性為 true使其可以。
expose字面意思就是 暴露。也就是說 我們需要允許暴露代理。
我們需要在Spring Boot啟動類上+一個注解:
@EnableAspectJAutoProxy(exposeProxy = true) @SpringBootApplication @MapperScan(basePackages = "com.codebear.Dao") public class SpringbootApplication {public static void main(String[] args) throws Exception {SpringApplication.run(SpringbootApplication.class, args);} }再次運行:
確實是開啟了兩個事務去執行的。
再看看數據庫,也沒有問題。
3. ApplicationContext:
@Service public class AccountSerivceImpl implements AccountService {@AutowiredAccountMapper mapper;@AutowiredApplicationContext context;AccountService service;@PostConstructprivate void setSelf() {service = context.getBean(AccountService.class);}@Transactional@Overridepublic void insertCodeBear() {try {service.insertCodeMonkey();} catch (Exception e) {e.printStackTrace();}Account account = new Account();account.setAccount("CodeBear");account.setPassword("CodeBear");mapper.insert(account);}@Transactional(propagation = Propagation.REQUIRES_NEW)@Overridepublic void insertCodeMonkey() {Account account = new Account();account.setAccount("CodeMonkey");account.setPassword("CodeMonkey");mapper.insert(account);int a = 1 / 0;} }驗證的圖片就省略了。
此方法不適用于prototype
在這里,我用了一個@PostConstruct注解,在初始化的時候,會調用被@PostConstruct標記的方法(注意,僅僅是初始化的時候,才會被調用。以后都不會被調用了,大家可以打個斷點試一下),這里這么做的目的就是為了提升一下效率,不用每次都getBean。所以如果這個類是prototype的,就不適用這個方法了。如果是prototype的話,就在insertCodeBear方法中使用getBean方法吧。
上兩種方法比較方便,沒有新建其他的接口或者是類,但是沒有很好的封裝獲得Aop代理對象的過程,也不是很符合 迪比特法則,也就是最少知識原則。
4. 重寫BeanPostProcessor接口:
關于這個接口是做什么的,這里就不詳細闡述了,簡單的來說這是Spring提供的接口,我們可以通過重寫它,在初始化Bean之前或者之后,自定義一些額外的邏輯。
首先,我們需要定義一個接口:
public interface WeavingSelfProxy {void setSelfProxy(Object bean); }要獲得代理對象的類,需要去實現它:
@Service public class AccountSerivceImpl implements AccountService, WeavingSelfProxy {@AutowiredAccountMapper mapper;AccountService service;@Overridepublic void setSelfProxy(Object bean) {System.out.println("進入到setSelfProxy方法");service = (AccountService) bean;}@Transactional@Overridepublic void insertCodeBear() {try {service.insertCodeMonkey();} catch (Exception e) {e.printStackTrace();}Account account = new Account();account.setAccount("CodeBear");account.setPassword("CodeBear");mapper.insert(account);}@Transactional(propagation = Propagation.REQUIRES_NEW)@Overridepublic void insertCodeMonkey() {Account account = new Account();account.setAccount("CodeMonkey");account.setPassword("CodeMonkey");mapper.insert(account);int a = 1 / 0;} }重寫BeanPostProcessor接口:
如果想學習Java工程化、高性能及分布式、深入淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給大家。
@Component public class SetSelfProxyProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if(bean instanceof WeavingSelfProxy){System.out.println("實現了WeavingSelfProxy接口");((WeavingSelfProxy) bean).setSelfProxy(bean);}return bean;} }這樣就可以了,驗證的圖片也省略了。
以上就是四種解決方案,可以說 各有千秋,沒有哪個好,哪個壞,只有適不適合。
如果想學習Java工程化、高性能及分布式、深入淺出。微服務、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級交流:854630135,群里有阿里大牛直播講解技術,以及Java大型互聯網技術的視頻免費分享給大家。
總結
以上是生活随笔為你收集整理的@postconstruct注解方法没有执行_被标记为事务的方法互相调用的坑(下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 提示tun虚拟网卡没有安装_Win10家
- 下一篇: 算法提高 01背包