Inject EJB into JAX-RS 2.0 subresource when subresource is got via ResourceContext


I am using Jersey 2.8 with Glassfish 4.0. I have a resource locator class which looks like below

@Path("/") @ManagedBean public class MyServiceLocator { @Context ResourceContext rc;//javax.ws.rs.container.ResourceContext @EJB private MyEJBHome myEJB; @Inject//javax.inject.Inject MySubService mss; @Path("/mysubservice") public MySubService getMySubService() { return rc.getResource(MySubService.class); //also tried return rc.initResource(new MySubService()); } }

and a sub resource class which is

@ManagedBean public class MySubService { @EJB public MyEJBHome myEJB; @Context HttpHeaders heads; /*@Inject private myEJBHome myEJB2;*/ @Path("/mypath") @GET @Produces(MediaType.APPLICATION_JSON) public Object doSomething(@Context SecurityContext securityContext) {...} }

beans.xml file is put to META-INF and WEB-INF.

In MyServiceLocator class private MyEJBHome myEJB is injected successfully. And MySubService mss object is injected successfully and with EJB injected into it.

<strong>The problem is that when MySubService is got via ResourceContext the EJB is not injected into it.</strong>

Previously i used Glassfish 3 and Jersey 1.17 with proprietary ResourceContext and absolutely the same code worked ok.

I googled a lot and read a lot of similar (but a bit different) questions and as i understood non JAX-RS stuff (EJB in my case) can't be injected when sub resource is got via ResorceContext. Is it true? If yes how can i work it around? The one possible solution is to inject sub resource objects to the resource locator class but there are too many of them and it seems to be very ugly.

<strong>EDIT</strong> Injection with @Inject works if to create a binder, bind ejb class to ejb interface and register that binder. But i don't want to describe binding for hundreds of my ejbs. Also as i understand it is specific binding fir HK2 system and i don't want to be linked to it.

Different actions with setting @Named annotations and trying to inject via CDI didn't help. It seems that when getting sub-resource via ResourceContext Jersey uses only HK2 and that's why CDI can't do it's work. Is that correct?


The only appropriate solution i found was to create my own annotation and inject provider.

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface EJBInject { String beanName(); } @Provider public class EjbInjectProvider implements InjectionResolver<EJBInject> { @Override public Object resolve(Injectee injectee, ServiceHandle<?> handle) { try { String beanName = injectee.getParent().getAnnotation(EJBInject.class).beanName(); return new InitialContext().lookup("java:global/MyApp/" + beanName); } catch (Exception e) { return null; } } @Override public boolean isConstructorParameterIndicator() { return false; } @Override public boolean isMethodParameterIndicator() { return false; } }

Then ejb can be injected using that annotation like

@EJBInject(beanName="MyBean") MyBeanEJBHome myBean;

In such case any standard EJB injections which MyBeanEJBHome might need work correctly, too.


  • jersey rest services showing exception javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalEx
  • Spring HttpMessageNotReadableException
  • JSONP with Jquery 1.9 and Jersey 2.5.1
  • jersey-client MessageBodyWriter not found for media type=application/x-www-form-urlencoded
  • Can't remove inline event handler in chrome
  • How to test method of JavaFX controller?
  • How to “remove”/“change” some require(…) calls when using browserify?
  • HttpServletRequest getPart(\") not found
  • Duplicated password validation messages ONLY IF new password is 1 char long
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • jinja2 template not found and internal server error
  • How secure are apple APNS push notifications?
  • How do I refer to a client_deploy.wsdd file that's in WEB-INF?
  • Why does the font in these TD elements render at different sizes?
  • Invalid object name 'dbo.Item'
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • TextToSpeech.setEngineByPackageName() triggers NullPointerException
  • Jquery UI tool tip close icon
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Java: can you cast Class into a specific interface?
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • AT Commands to Send SMS not working in Windows 8.1
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Is there a mandatory requirement to switch app.yaml?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Angular 2 constructor injection vs direct access
  • coudnt use logback because of log4j
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • UserPrincipal.Current returns apppool on IIS
  • Converting MP3 duration time