HK2 Jersey EJB 3 injection

廉价感情. 提交于 2020-01-06 11:48:09

问题


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?

来源:https://stackoverflow.com/questions/32811338/hk2-jersey-ejb-3-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!