2017-10-18 148 views
0

我有一个服务,它看起来像这样:@PersistenceContext在自动装配Autowired Construcor春季

@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构造函数? 所以问题是,我可以单独注释构造函数参数entityManager,同时保留@Autowired注释?

回答

0

我以前有过这个问题,但不幸的是我不认为你可以使用注释@Autowired在构造函数中注入EntityManager。如果你需要有EntityManager的构造函数

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

+0

好的。所以你知道这个解决方法吗? – Neeraj

+0

不幸的是,如果你想使用Spring DI和annotations注入,你唯一的解决方案是在域EntityManager上使用注解@PersistenceContext。 – db80

0

我不知道:看到这个票。其实,你不能这样做。 您可以查看详细的解决方案here

相关问题