I would like to inject a buisiness service bean in a sub resource which is defined in a dedicated class and delivered by a sub resource locator.
Some example code:
A 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);
}
}
The corresponding sub 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 won't CDI let invoke the dependencies... Note that the resources are managed objects. Otherwise it wouldn't even be possible to inject a bean in a root resource (here I'm pushing my other questions' view count to get more opinions ;-))!
I tried everything I can think of but it just won't work...
Currently I'm using the libraries that are shipped with glassfish 4.
And of course, thank you in advance (almost forgot that)!
Okay, I figured it out.
It is really kind of stupid. Sometimes you have to roll back completely.
There must have been something wrong with my initial attempt (typo, left out something...I cannot reproduce it, whatever).
I slightly changed the root resource from above:
@RequestScoped
@Path("service")
public class MyResource {
@Inject MySubResource mySubResource;
// Sub resource locator
@Path("subservice")
public MySubResource locateToSubResource () {
return mySubResource;
}
}
Yes, that's it. I must admit, that's the most intuitive solution one can imagine and if such an approach don't work one must have done something wrong... Dont't ask me what exactly was the cause.
I guess it's as always - sleep deprivation let people turn into morons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With