Correct way to Integrate JAX-RS with CDI?

眉间皱痕 提交于 2019-11-28 14:06:57

You do not need to set the Path annotation on your AccountService CDI bean. If CDI is enabled on your application (either with empty beans.xml in CDI 1.0 or discovery-mode=all in CDI > 1.0), you can @Inject any CDI bean in your JAX-RS resource. So you just have to write the following class:

@Path("/")
public class ServiceResource {
   @Inject
   private AccountService accountService;

   @GET
   @Path("/account/get")
   public Account getAccount(@QueryParam("id") String id) {
     return accountService.get(id);
   }
}

@javax.inject.Singleton
public class AccountService {
   public void Account get(String id){...}
}

The article you linked in your post deals with mixing EJB and CDI annotations. For example you can mix @Stateless and @Path annotations. It's interesting for example because you can :

  • Benefit of EJB transaction in your Rest resource (even if now you can use @Transactional interceptor binding)
  • Set a pool of resources
  • etc.

Note that all of this works without the help of deltaspike dependency.

For your second question, as Quartz manages its own threads, classes are not handled by CDI so you can not inject beans in Quartz classes. The aim of the deltaspike module is to allow injecting CDI beans in Quartz Jobs. Internally, deltaspike controls CDI Contexts.

EDIT

For your last questions:

  • Your HK2 problem comes pretty sure from a missing dependency (in your application or server). As said in a previous comment, I managed to deploy your App on Glassfish 4 (build 89) with the source files you provided.

  • Regarding the integration of CDI with Quartz, I think the best is to implement your own JobFactory and instanciate your jobs using BeanManager. Take a look at this link : https://devsoap.com/injecting-cdi-managed-beans-into-quarz-jobs/

First of all injected resources(beans) and Jersey Endpoint class(point of injection) must be CDI-Aware. It must be detecteable by CDI. We can use bean-discovery-mode="all" - then CDI scan ALL classes or bean-discovery-mode="annotated" and MARK our class with PROPER annotation: from here : Bean defining annotations. I prefer@Dependent or @RequestScoped

Then we must use Jersey Extension

<dependency>
  <groupId>org.glassfish.jersey.ext.cdi</groupId>
  <artifactId>jersey-cdi1x-servlet</artifactId>
  <version>{version}</version>
  <scope>runtime</scope>
</dependency>

`

to connect CDI with HK2 discovery mechanism. Here is Official oracle Guideline

The default beans.xml discovery-mode (in Java EE 7) is "annotated". Which means only beans that have CDI annotations are recognized and managed by CDI.

Your AccountJob class is not annotated. If you want CDI to be able to inject the service into it then you need to annotate it with some scope annotation, e.g. @ApplicationScoped.

Your other option is to create CDI producer for creating AccountJob beans. See: http://docs.jboss.org/weld/reference/latest/en-US/html_single/#_producer_methods

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