@postconstruct注解方法没有执行_被标记为事务的方法互相调用的坑(下)
上一節(jié),主要分析了 被標(biāo)記為事務(wù)的方法互相調(diào)用,事務(wù)失效的原因,思考比較多,這一節(jié)主要說(shuō)說(shuō)解決方案,思考會(huì)少一些。
解決方案的核心: 通過(guò)代理對(duì)象去調(diào)用方法
1.把方法放到不同的類(lèi):
如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。
我們需要新建一個(gè)接口:
public interface OtherService {void insertCodeMonkey(); }再定義一個(gè)類(lèi)去實(shí)現(xiàn)這個(gè)接口:
@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;} }修改原本的實(shí)現(xiàn)類(lèi):
@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);} }運(yùn)行,查看數(shù)據(jù)庫(kù):
只有一條數(shù)據(jù),insertCodeBear方法執(zhí)行成功了,insertCodeMonkey執(zhí)行失敗,并且回滾了。
讓我們?cè)倏纯纯刂婆_(tái)的日志:
如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。
可以看到是開(kāi)了兩個(gè)事務(wù)去執(zhí)行的。
這種解決方案最簡(jiǎn)單,不需要了解其他東西,但是這種方案需要修改代碼結(jié)構(gòu),本來(lái)兩個(gè)方法都是屬于同一個(gè)類(lèi)的,現(xiàn)在需要強(qiáng)行把它們拆開(kāi)。
2. AopContext:
我們的目標(biāo)是要在實(shí)現(xiàn)類(lèi)中獲取本類(lèi)的代理對(duì)象,Spring提供了Aop上下文,即:AopContext,通過(guò)AopContext,可以很方便的獲取到代理對(duì)象:
@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;} }當(dāng)寫(xiě)好代碼,很愉快的去測(cè)試,發(fā)現(xiàn)竟然報(bào)錯(cuò)了:
翻譯下:不能找到當(dāng)前的代理,需要設(shè)置exposeProxy屬性為 true使其可以。
expose字面意思就是 暴露。也就是說(shuō) 我們需要允許暴露代理。
我們需要在Spring Boot啟動(dòng)類(lèi)上+一個(gè)注解:
@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);} }再次運(yùn)行:
確實(shí)是開(kāi)啟了兩個(gè)事務(wù)去執(zhí)行的。
再看看數(shù)據(jù)庫(kù),也沒(méi)有問(wèn)題。
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;} }驗(yàn)證的圖片就省略了。
此方法不適用于prototype
在這里,我用了一個(gè)@PostConstruct注解,在初始化的時(shí)候,會(huì)調(diào)用被@PostConstruct標(biāo)記的方法(注意,僅僅是初始化的時(shí)候,才會(huì)被調(diào)用。以后都不會(huì)被調(diào)用了,大家可以打個(gè)斷點(diǎn)試一下),這里這么做的目的就是為了提升一下效率,不用每次都getBean。所以如果這個(gè)類(lèi)是prototype的,就不適用這個(gè)方法了。如果是prototype的話,就在insertCodeBear方法中使用getBean方法吧。
上兩種方法比較方便,沒(méi)有新建其他的接口或者是類(lèi),但是沒(méi)有很好的封裝獲得Aop代理對(duì)象的過(guò)程,也不是很符合 迪比特法則,也就是最少知識(shí)原則。
4. 重寫(xiě)B(tài)eanPostProcessor接口:
關(guān)于這個(gè)接口是做什么的,這里就不詳細(xì)闡述了,簡(jiǎn)單的來(lái)說(shuō)這是Spring提供的接口,我們可以通過(guò)重寫(xiě)它,在初始化Bean之前或者之后,自定義一些額外的邏輯。
首先,我們需要定義一個(gè)接口:
public interface WeavingSelfProxy {void setSelfProxy(Object bean); }要獲得代理對(duì)象的類(lèi),需要去實(shí)現(xiàn)它:
@Service public class AccountSerivceImpl implements AccountService, WeavingSelfProxy {@AutowiredAccountMapper mapper;AccountService service;@Overridepublic void setSelfProxy(Object bean) {System.out.println("進(jìn)入到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;} }重寫(xiě)B(tài)eanPostProcessor接口:
如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。
@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("實(shí)現(xiàn)了WeavingSelfProxy接口");((WeavingSelfProxy) bean).setSelfProxy(bean);}return bean;} }這樣就可以了,驗(yàn)證的圖片也省略了。
以上就是四種解決方案,可以說(shuō) 各有千秋,沒(méi)有哪個(gè)好,哪個(gè)壞,只有適不適合。
如果想學(xué)習(xí)Java工程化、高性能及分布式、深入淺出。微服務(wù)、Spring,MyBatis,Netty源碼分析的朋友可以加我的Java高級(jí)交流:854630135,群里有阿里大牛直播講解技術(shù),以及Java大型互聯(lián)網(wǎng)技術(shù)的視頻免費(fèi)分享給大家。
總結(jié)
以上是生活随笔為你收集整理的@postconstruct注解方法没有执行_被标记为事务的方法互相调用的坑(下)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 提示tun虚拟网卡没有安装_Win10家
- 下一篇: 算法提高 01背包