参考:https://www.iteye.com/topic/1122740数据库
上一节,主要分析了 被标记为事务的方法互相调用,事务失效的缘由,思考比较多,这一节主要说说解决方案,思考会少一些。bash
咱们须要新建一个接口:app
public interface OtherService {
void insertCodeMonkey();
}
复制代码
再定义一个类去实现这个接口:ide
@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;
}
}
复制代码
修改本来的实现类:post
@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);
}
}
复制代码
运行,查看数据库: 测试
让咱们再看看控制台的日志: ui
能够看到是开了两个事务去执行的。spa
这种解决方案最简单,不须要了解其余东西,可是这种方案须要修改代码结构,原本两个方法都是属于同一个类的,如今须要强行把它们拆开。prototype
咱们的目标是要在实现类中获取本类的代理对象,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;
}
}
复制代码
当写好代码,很愉快的去测试,发现居然报错了:
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);
}
}
复制代码
再次运行:
确实是开启了两个事务去执行的。
再看看数据库,也没有问题。
@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代理对象的过程,也不是很符合 迪比特法则,也就是最少知识原则。
关于这个接口是作什么的,这里就不详细阐述了,简单的来讲这是Spring提供的接口,咱们能够经过重写它,在初始化Bean以前或者以后,自定义一些额外的逻辑。 首先,咱们须要定义一个接口:
public interface WeavingSelfProxy {
void setSelfProxy(Object bean);
}
复制代码
要得到代理对象的类,须要去实现它:
@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;
}
}
复制代码
重写BeanPostProcessor接口:
@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("实现了WeavingSelfProxy接口");
((WeavingSelfProxy) bean).setSelfProxy(bean);
}
return bean;
}
}
复制代码
这样就能够了,验证的图片也省略了。
以上就是四种解决方案,能够说 各有千秋,没有哪一个好,哪一个坏,只有适不适合。