Springboot同一Server类方法调用事务解决方案
1、引入springboot-aop start
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
主要是使用里面的动态代理
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>compile</scope>
</dependency>
2、开启动态代理
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableDiscoveryClient
@SpringBootApplication
public class FamilyBookingApplication {
public static void main(String[] args) {
SpringApplication.run(FamilyBookingApplication.class, args);
}
}
@EnableAspectJAutoProxy(exposeProxy = true):
开启aspectj动态代理功能。以后所有的动态代理都是aspectj对象暴露代理对象。
3、本类互调用代理对象调用
@Service("userService")
public class UserServiceImpl implements userService {
@Transactional
public void a(){
UserService userService = (UserService)AopContext.currentProxy();
userService.b();
userService.c();
}
@Transactional
public void b(){
}
@Transactional
public void c(){
}
}
评论 (0)