-->

HK2 Jersey EJB 3 injection

2019-09-14 01:06发布

问题:

I followed the 22.1. Implementing Custom Injection Provider paragraph

https://jersey.java.net/documentation/latest/user-guide.html#deployment

I was able to develop my own injectable custom annotation following the steps described in this post:

Jersey 2.x Custom Injection annotation NULL

So i use these deployment instructions to bind my injectionresolver:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param> 
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.klopotek.klas.auth.injection.PrincipalConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Now i'm able to get my object injected into my custom annotation inside my rest service:

@Path("modelORA")
public class ModelRetrieverORA {

   @Context
   SecurityContext securityContext;

   @Context
   private UriInfo uriInfo;

   @MyAnnot    
   private Myinjectable Principal;

Both @Context and @MyAnnot works fine as @Context annotation is handled inside Jersey Container.

Now i want to use JPA and JTA for my persistence layer. My final code should look like:

 @Path("modelORA")
public class ModelRetrieverORA {

   @PersistenceContext (unitname="myName")
   EntityManager manager;

   @Resource
   UserTransaction transaction;

   @Context
   SecurityContext securityContext;

   @Context
   private UriInfo uriInfo;

   @MyAnnot    
   private Myinjectable Principal;

But of course both manager and transaction are null. The injection doesn't happen.

I think the issue is related to either the HK2 injection engine used by Jersey2 (i need it because of my custom annotation) or CDI engine in my application server (tomcat or wildfly 9). So how can i say to the WELD or whatever CDI engine to inject transaction and JPA manager?