Jersey: How to introduce EJB into an additional resource?

I would like to add the buisiness bean service to an additional resource, which is defined in the allocated class and delivered by the auxiliary resources locator.

Code example:

  • Root resource

    @RequestScoped
    @Path("service")
    public class MyResource {
    
        @Context
        ResourceContext resourceContext;
    
        // Sub resource locator
        @Path("subservice")
        public MySubResource locateToSubResource () {
            // I don't want to create it myself.
            return resourceContext.getResource(MySubResource.class);
        }
    }
    
  • Corresponding resource

    @RequestScoped
    public class MySubResource {
    
        // Note that businessBean itself consists of
        // multiple ejbs that also need to be injected so that it can do its job!
        @Inject
        private BusinessBean businessBean; 
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String get () {
            return businessBean.doStuff();
        }
    }
    

Jersey will not have CDI to cause dependencies ... Note that resources are managed objects. Otherwise, it would even be impossible to insert a bean in the root resource ( here I push the number of views of my other questions to get more opinions ;-) )

I tried everything I could, but it just won't work ...

I am currently using libraries that come with glass fish 4.

And, of course, thank you in advance (almost forgot)!

+4
2

, .

. .

, - (, - ... , ).

:

@RequestScoped
@Path("service")
public class MyResource {

    @Inject MySubResource mySubResource;

    // Sub resource locator
    @Path("subservice")
    public MySubResource locateToSubResource () {
        return mySubResource;
    }
}

, . , , , , - ... , .

, - .

+7

:

public SubResource subResource() {
    return CDI.current().select(SubResource.class).get();
}
+2

Source: https://habr.com/ru/post/1524180/


All Articles