我的问题是我无法从 GenericManagerJPA<> 注入(inject)多个模板实例。
也就是说,在这段代码中,projectManager 和 userManager 都会包含相同的 GenericManagerJPA<User>
实例。我不知道为什么...
@Stateless
public class UserFacadeJPA implements Serializable {
@Inject private GenericManagerJPA<Project> projectManager;
@Inject private GenericManagerJPA<User> userManager;
@PostConstruct
public void init() {
projectManager.setEntityClass(Project.class);
userManager.setEntityClass(User.class);
}
....
来自这个类(class):
@Dependent
@Stateless
public class GenericManagerJPA<T> implements Serializable {
...
如何使用 CDI 注入(inject)通用对象的不同实例? 你能帮我解决这个问题吗?非常感谢。
PS:我已经阅读了这篇文章( https://community.jboss.org/blogs/scott.stark/2012/08/21/a-generic-producer-method ),它介绍了如何创建它(我认为),但我认为这对我来说有点难以理解。因此,如果您有其他解决方案或者您可以更清楚地向我解释原理...
最佳答案
您遇到的是 EJB 问题,而不是真正的 CDI 问题。如果从 GenericManagerJPA
类中删除 @Stateless
,问题就会消失。不幸的是,EJB 不能很好地处理泛型。
关于java - CDI : inject two generics from the same template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17697333/