參考:https://www.iteye.com/topic/1122740
上一節(jié),主要分析了 被標記為事務的方法互相調用,事務失效的原因,思考比較多,這一節(jié)主要說說解決方案,思考會少一些。
解決方案的核心: 通過代理對象去調用方法
1.把方法放到不同的類:
我們需要新建一個接口:
public interface OtherService {
void insertCodeMonkey();
}
再定義一個類去實現(xiàn)這個接口:
@Service
public class OtherServiceImpl implements OtherService {
@Autowired
AccountMapper 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;
}
}
修改原本的實現(xiàn)類:
@Service
public class AccountSerivceImpl implements AccountService {
@Autowired
AccountMapper mapper;
@Autowired
OtherService otherService;
@Transactional
@Override
public void insertCodeBear() {
try {
otherService.insertCodeMonkey();
} catch (Exception e) {
e.printStackTrace();
}
Account account = new Account();
account.setAccount("CodeBear");
account.setPassword("CodeBear");
mapper.insert(account);
}
}
運行,查看數(shù)據(jù)庫:

只有一條數(shù)據(jù),insertCodeBear方法執(zhí)行成功了,insertCodeMonkey執(zhí)行失敗,并且回滾了。
讓我們再看看控制臺的日志:

可以看到是開了兩個事務去執(zhí)行的。
這種解決方案最簡單,不需要了解其他東西,但是這種方案需要修改代碼結構,本來兩個方法都是屬于同一個類的,現(xiàn)在需要強行把它們拆開。
2. AopContext:
我們的目標是要在實現(xiàn)類中獲取本類的代理對象,Spring提供了Aop上下文,即:AopContext,通過AopContext,可以很方便的獲取到代理對象:
@Service
public class AccountSerivceImpl implements AccountService {
@Autowired
AccountMapper mapper;
@Transactional
@Override
public 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)
@Override
public void insertCodeMonkey() {
Account account = new Account();
account.setAccount("CodeMonkey");
account.setPassword("CodeMonkey");
mapper.insert(account);
int a = 1 / 0;
}
}
當寫好代碼,很愉快的去測試,發(fā)現(xiàn)竟然報錯了:

翻譯下:不能找到當前的代理,需要設置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);
}
}
再次運行:

確實是開啟了兩個事務去執(zhí)行的。
再看看數(shù)據(jù)庫,也沒有問題。
3. ApplicationContext:
@Service
public class AccountSerivceImpl implements AccountService {
@Autowired
AccountMapper mapper;
@Autowired
ApplicationContext context;
AccountService service;
@PostConstruct
private void setSelf() {
service = context.getBean(AccountService.class);
}
@Transactional
@Override
public 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)
@Override
public 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. 重寫B(tài)eanPostProcessor接口:
關于這個接口是做什么的,這里就不詳細闡述了,簡單的來說這是Spring提供的接口,我們可以通過重寫它,在初始化Bean之前或者之后,自定義一些額外的邏輯。
首先,我們需要定義一個接口:
public interface WeavingSelfProxy {
void setSelfProxy(Object bean);
}
要獲得代理對象的類,需要去實現(xiàn)它:
@Service
public class AccountSerivceImpl implements AccountService, WeavingSelfProxy {
@Autowired
AccountMapper mapper;
AccountService service;
@Override
public void setSelfProxy(Object bean) {
System.out.println("進入到setSelfProxy方法");
service = (AccountService) bean;
}
@Transactional
@Override
public 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)
@Override
public void insertCodeMonkey() {
Account account = new Account();
account.setAccount("CodeMonkey");
account.setPassword("CodeMonkey");
mapper.insert(account);
int a = 1 / 0;
}
}
重寫B(tài)eanPostProcessor接口:
@Component
public class SetSelfProxyProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof WeavingSelfProxy){
System.out.println("實現(xiàn)了WeavingSelfProxy接口");
((WeavingSelfProxy) bean).setSelfProxy(bean);
}
return bean;
}
}
這樣就可以了,驗證的圖片也省略了。
以上就是四種解決方案,可以說 各有千秋,沒有哪個好,哪個壞,只有適不適合。