Inject PersistenceContext with CDI

后端 未结 2 1430
孤城傲影
孤城傲影 2020-12-19 22:31

Currently, I\'m using PersistenceContext to inject an EntityManager. The EM is injected perfectly.

@Stateless
public StatelessSessionBean implements Stateles         


        
2条回答
  •  臣服心动
    2020-12-19 22:46

    @PersistenceContext is an EJB injection point, while @Inject is a CDI one

    Actually, no. @PersistenceContext annotation can be used in CDI and is not connected with EJB. You can do something like this:

    @Named
    public class EntityDAO {
        @PersistenceContext
        private EntityManager manager;
    
        ...
    
    }
    

    EJB uses @EJB annotation to inject other EJB, but it can inject any CDI bean or persistence context with same annotations.

提交回复
热议问题