我有一个看起来像这样的服务:
@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;
}
}
我需要用@PersistenceContext
注释EntityManager
.我怎样才能做到这一点,同时保留@Autowired
构造函数?所以问题是,我可以在保留@Autowired
注释的同时单独注释构造函数参数entityManager
吗?
我以前遇到过这个问题,不幸的是,我认为您无法使用注释@Autowired
在构造函数中注入实体管理器。请参阅此票:
https://jira.spring.io/browse/SPR-10443
我不确定您是否需要在构造函数中使用实体管理器。实际上,你做不到。您可以在此处查看详细的解决方案。