【发布时间】:2019-02-07 09:40:47
【问题描述】:
我对 Spring 的 @Transactional 在内部如何工作很感兴趣,但是在我读到它的任何地方都有代理的概念。代理应该被自动装配以代替真正的 bean,并使用额外的事务处理方法“装饰”基本方法。 这个理论对我来说非常清楚并且非常有意义,所以我试图检查它是如何运作的。 我创建了一个带有基本控制器和服务层的 Spring Boot 应用程序,并用 @Transactional 注释标记了一个方法。服务如下所示:
public class TestService implements ITestService {
@PersistenceContext
EntityManager entityManager;
@Transactional
public void doSomething() {
System.out.println("Service...");
entityManager.persist(new TestEntity("XYZ"));
}}
控制器调用服务:
public class TestController {
@Autowired
ITestService testService;
@PostMapping("/doSomething")
public ResponseEntity addHero() {
testService.doSomething();
System.out.println(Proxy.isProxyClass(testService.getClass()));
System.out.println(testService);
return new ResponseEntity(HttpStatus.OK);
}}
一切正常,新实体被持久化到数据库中,但我关心的重点是输出:
Service...
false
com.example.demo.TestService@7fb48179
似乎是显式注入了服务类而不是代理类。不仅“isProxy”返回 false,而且类输出(“com.example.demo.TestService@7fb48179”)表明它不是代理。
你能帮我解决这个问题吗?为什么没有注入代理,没有代理它是如何工作的?有什么办法可以“强制”它被代理,如果是这样 - 为什么 Spring 默认不注入代理?
没有什么要补充的,这是一个非常简单的应用程序。应用程序属性也没有什么花哨的:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=superSecretPassword
spring.datasource.url=jdbc:mysql://localhost:3306/heroes?serverTimezone=UTC
spring.jpa.hibernate.ddl-auto=create-drop
提前谢谢你!
【问题讨论】:
标签: java spring hibernate jpa transactional