EJB 3.1 @EJB Injection into POJO

前端 未结 4 1318
清歌不尽
清歌不尽 2020-12-05 08:22

With the new EJB 3.1 spec is it possible to inject an EJB into a pojo? I know in EJB 3.0 the @EJB annotation could be used to inject an EJB but this did not work on simple

4条回答
  •  独厮守ぢ
    2020-12-05 09:02

    I wonder too if I could inject EJBs into unmanaged objects. See the Weld (JSR 299 reference implementation) documentation for more details.

    But you can perform dependency injection by hand inside a repository or factory like this:

    @Stateless
    public PojoRespository {
    
      @Inject
      ResourceForPojos resource;
      @PersistenceContext
      private EntityManager em;
    
      public Pojo findById(Object id) {
        Pojo p = (Pojo) em.find(Pojo.class, id);
        p.setResource(resource); // injects resource
        return p;
      }
    
    }
    

    If you have many methods where injection should be performed, you could use an interceptor.

提交回复
热议问题