这两种选择有什么区别……它们可以互换使用吗?
(A) 创建自定义注解,以便可以在 DAO 中使用 @Inject 代替 @PersistenceContext,如 - how-to-stack-custom-annotation-in-java-with-inject-annotation的答案所示
(B) 使用@Named("yourName") 来限定Producer,例如下面的代码示例。
public class Resources {
/**
* EntityManager's persistence context is defined here so the @Inject annotation may be used in referencing classes.
*/
@Produces
@Named("MyEm")
@PersistenceContext(unitName = "jboss.managed")
private EntityManager em;
}
@Stateless
public class FiletracksentHome {
..
@Inject
@Named("MyEm")
private EntityManager entityManager;
..
}