Spring中Autowired Construcor中的@PersistenceContext

时间:2017-10-18 09:27:35

标签: java spring

我有一个看起来像这样的服务:

@Service
public class MyService {

private final EntityManager entityManager;

private final SomeBean someBean;

@Autowired
public DbServiceImpl(EntityManager entityManager, SomeBean someBean) {
    this.entityManager = entityManager;
    this.someBean = someBean;
}
}

我需要使用EntityManager注释@PersistenceContext。在保持@Autowired构造函数的同时,我该怎么做? 所以问题是,我可以单独注释构造函数参数entityManager,同时保留@Autowired注释吗?

2 个答案:

答案 0 :(得分:1)

之前我遇到过这个问题,不幸的是我不认为你可以使用注释@Autowired在构造函数中注入EntityManager。看到这张票:

https://jira.spring.io/browse/SPR-10443

答案 1 :(得分:0)

我不确定您是否需要在构造函数中使用EntityManager。实际上,你不能这样做。 您可以查看详细解决方案here