-
1. Re: Injecting an EJB from a jar into a jax-rs class in a war
skunk Dec 22, 2009 12:03 PM (in response to skunk)Some things I have tried:
---Deploying as a War---
I made the ejb-jar the library of a war containing the facelet and jax-rs. There was no change in the outcome - the facelet worked but jax-rs didn't.
---Making the JAX-RS class available to EL---
by adding @Named to the jax-rs class it could be accessed via EL, and then injection mysteriously works if accessed by the facelet (e.g. with #{rootResource.xml})
However, it still didn't work when accessing the resource 'natively'.
-
2. Re: Injecting an EJB from a jar into a jax-rs class in a war
nickarls Dec 22, 2009 12:03 PM (in response to skunk)Strange. Can you inject the RootResource into something else? Just curious about the visibility. I don't think the JAX-RS annotations should matter.
-
3. Re: Injecting an EJB from a jar into a jax-rs class in a war
skunk Dec 22, 2009 12:29 PM (in response to skunk)Hi Nik, thank you for your reply.
I think our last messages crossed - 'injecting' into a facelet (if that is the right language) does work, it's just accessing the jax-rs resource directly that doesn't.
I've just tried adding another layer of indirection. I've added a third class which injects the jax-rs class:
@Named public class TertiaryResource { @Inject RootResource rootResource; public Collection<Identifier> getIdents() { return rootResource.getXml(); } }
And another facelet to test it:
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h1>Identifiers</h1> <p>Service bean: #{tertiaryResource}</p> #{tertiaryResource.idents} </h:body> </html>
So now the flow is:
Request to facelet -> tertiary resource -> root resource -> identifier service.
This works, but doesn't do anything useful.
This doesn't work:
Request to JAX-RS root resource -> identifier service
It seems weld doesn't manage jax-rs classes unless they are accessed in a specific way. Do I need to add some sort of filter or interceptor to web.xml to have this work?
Thanks.
-
4. Re: Injecting an EJB from a jar into a jax-rs class in a war
nickarls Dec 22, 2009 12:51 PM (in response to skunk)Hmm. I must stand down on this issue with a
dunno
, I haven't done anything with CDI and JAX-RS. There is of course the chance that the server creates a new instance of the class all by itself (which will of course have no injections). I'm sure there will be helpers and patterns for optimal usage but until then: can you access BeanManager from JNDI and get hold of the stuff yourself? -
5. Re: Injecting an EJB from a jar into a jax-rs class in a war
alin.heyoulin.qq.com Dec 22, 2009 1:08 PM (in response to skunk)yep,it is probably IdentifierService has created by jax-rs servlet not by injected.
-
6. Re: Injecting an EJB from a jar into a jax-rs class in a war
skunk Dec 22, 2009 1:57 PM (in response to skunk)Ok, changing the jax-rs class to look as below and not use CDI at all does work, but it's not the elegance I was hoping for. CDI works for all classes, except where it doesn't?
@Path("/") public class RootResource { public RootResource() throws NamingException { identifierService = (IdentifierService) (new InitialContext()).lookup("java:global/WebApplication1/IdentifierService"); } //@Inject IdentifierService identifierService; @GET @Produces("application/xml") public Collection<Identifier> getXml() { return identifierService.getIdentifiers(); } }
-
7. Re: Injecting an EJB from a jar into a jax-rs class in a war
nickarls Dec 22, 2009 2:03 PM (in response to skunk)It works in those lifecycles where it's specified to work. If you do a new Foo(), you won't get CDI in there either.
Using BeanManager might still be worth a shot. While there are a few more steps for resolving stuff, you get injections from that point in (if the thing you resolve has other CDI injections)
-
8. Re: Injecting an EJB from a jar into a jax-rs class in a war
alin.heyoulin.qq.com Dec 22, 2009 2:10 PM (in response to skunk)you can try @Path("/") public class RootResource { public RootResource() throws NamingException { BeanManager beanManager=(BeanManager)new InitialContext().lookup("java:comp/BeanManager"); Bean<IdentifierService> identifierServiceBean= (Bean<IdentifierService>)beanManager.getBeans(Conversation.class).iterator().next(); identifierService= (IdentifierService)beanManager.getReference(identifierServiceBean, IdentifierService.class, beanManager.createCreationalContext(identifierServiceBean)); } //@Inject IdentifierService identifierService; @GET @Produces("application/xml") public Collection<Identifier> getXml() { return identifierService.getIdentifiers(); } }
-
9. Re: Injecting an EJB from a jar into a jax-rs class in a war
nickarls Dec 22, 2009 2:24 PM (in response to skunk)ITYM getBeans(IdentifierService.class...) and not getBeans(Conversation.class...)?
-
10. Re: Injecting an EJB from a jar into a jax-rs class in a war
alin.heyoulin.qq.com Dec 22, 2009 2:30 PM (in response to skunk):) Soory,my code copy and paste and then edit. you can try @Path("/") public class RootResource { public RootResource() throws NamingException { BeanManager beanManager=(BeanManager)new InitialContext().lookup("java:comp/BeanManager"); Bean<IdentifierService> identifierServiceBean= (Bean<IdentifierService>)beanManager.getBeans(IdentifierService.class).iterator().next(); identifierService= (IdentifierService)beanManager.getReference(identifierServiceBean, IdentifierService.class, beanManager.createCreationalContext(identifierServiceBean)); } //@Inject IdentifierService identifierService; @GET //@Produces("application/xml") public Collection<Identifier> getXml() { return identifierService.getIdentifiers(); } }
-
11. Re: Injecting an EJB from a jar into a jax-rs class in a war
skunk Dec 22, 2009 3:52 PM (in response to skunk)Thanks guys, that looks useful for injecting things which aren't EJBs.
For my particular case with a stateless EJB, because there isn't going to be a CDI context coming from the JAX-RS request anyway, I might as well just use the simpler EJB lookup?
Supposing I want access to a session scoped bean (Identity is a good candidate), is there someway I could tell the bean manager the users' session (from the jsessionid) and get back a bean relating to that user?
Does he youlin's code magically do that? Let me know if I'm not making sense!
Thanks!
Dave -
12. Re: Injecting an EJB from a jar into a jax-rs class in a war
alin.heyoulin.qq.com Dec 22, 2009 5:02 PM (in response to skunk)I think you'd better wrappe EJBs into weld beans and use BeanManager to get the weld beans or you can wait for weld-jax-rs inetgration module.
-
13. Re: Injecting an EJB from a jar into a jax-rs class in a war
nickarls Dec 23, 2009 7:09 AM (in response to skunk)I guess you would also need (in any case) to activate the contexts with calls to methods in org.jboss.weld.servlet.ServletLifeCycle...
-
14. Re: Injecting an EJB from a jar into a jax-rs class in a war
gavin.king Dec 27, 2009 3:10 AM (in response to skunk)IMO the original code example should work, according to section 6.2 of the JAX-RS specification. Try asking in the glassfish forums, or posting a bug against glassfish.