-
1. Re: Clarification on what can be injected and where it can be?
meetoblivion Jan 5, 2010 1:29 AM (in response to hirowla.ian.rowlands.three.com.au)For a bean to be eligible for injection (injected into another object), it has to be a managed bean or a session bean. It needs to have Qualifiers. You can also use a producer to create instances of beans.
Almost everything within the EE landscape can be injected in to - all EJBs, ManagedBeans, Servlet stack. Without seeing some code sample, it's hard to tell what might be the problem.
-
2. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 5, 2010 2:23 AM (in response to hirowla.ian.rowlands.three.com.au)Are you sure that for a bean to be eligible for injection (injected into another object), it has to be a managed bean or a session bean? As I said, I can inject the same bean into a Managed bean but not into the other objects. The bean being injected is the same, the destination is different.
Here's some relevant code. Firstly, the recipient of the code (the important bits):
public class ApplicationInitialisation implements ServletContextListener { @Inject private Logger log; public void contextInitialized(ServletContextEvent event) { System.out.println("Logger should be non-null here"); } public void contextDestroyed(ServletContextEvent event) {} }
Now the actual producer of the logger:
public class LogFactory { @Produces Logger createLogger(InjectionPoint injectionPoint) { return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); } }
This injection does work into a @Named JSF bean.
-
3. Re: Clarification on what can be injected and where it can be?
meetoblivion Jan 5, 2010 2:26 AM (in response to hirowla.ian.rowlands.three.com.au)What are you deploying to? Last I heard, Servlet level injection doesn't work on Jetty.
-
4. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 5, 2010 2:41 AM (in response to hirowla.ian.rowlands.three.com.au)I'm using JBoss 6.0.0 M1.
-
5. Re: Clarification on what can be injected and where it can be?
meetoblivion Jan 5, 2010 2:50 AM (in response to hirowla.ian.rowlands.three.com.au)that also doesn't support servlet injection.
-
6. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 5, 2010 2:56 AM (in response to hirowla.ian.rowlands.three.com.au)That's not entirely clear from Chapter 18 of the documentation. Can you clarify where it says that?
-
7. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 5, 2010 2:57 AM (in response to hirowla.ian.rowlands.three.com.au)Also, it doesn't clarify why I'm not getting it as part of my EJB interceptor.
-
8. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 5, 2010 3:11 AM (in response to hirowla.ian.rowlands.three.com.au)Just something I noticed as well. In that ServletContextListener, I was injecting an EJB using @EJB and everything works. However if I use @Inject, the field is null as well.
-
9. Re: Clarification on what can be injected and where it can be?
meetoblivion Jan 5, 2010 3:25 AM (in response to hirowla.ian.rowlands.three.com.au)Servlet 3.0 is required for CDI to work for servlets. It's not clear, but it's been discussed a few times in the forums.
-
10. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 5, 2010 4:36 AM (in response to hirowla.ian.rowlands.three.com.au)I can sort of understand for the ServletContextListener, but not for the EJB interceptor.
-
11. Re: Clarification on what can be injected and where it can be?
gavin.king Jan 5, 2010 6:14 AM (in response to hirowla.ian.rowlands.three.com.au)
Ian R wrote on Jan 05, 2010 01:13:- A class that implements a ServletContextListener. The member variable is null when I go into it.
- An object class that I'm using as an EJB interceptor (as an exception fault barrier). Same thing, the member variable is null.
The CDI specification requires that you can inject into both these kinds of things. But the only container which currently provides full support for Java EE 6 is GlassFish. So this stuff may not work in other environments.
-
12. Re: Clarification on what can be injected and where it can be?
nickarls Jan 5, 2010 8:33 AM (in response to hirowla.ian.rowlands.three.com.au)I'm not quite sure on the Servlet version in 6.0M1 as it appears to have some 3.0 features but some of the classes are not identical to those in latest GF (as I recall). In any case, it is the servlet engine that has to take care of injection into context listeners as it is the one creating the listeners, I guess...
But please give some more code that sheds light on the EJB interceptor issue if you can.
-
13. Re: Clarification on what can be injected and where it can be?
meetoblivion Jan 5, 2010 11:28 AM (in response to hirowla.ian.rowlands.three.com.au)I think the important thing to point out is that EJB Injection isn't the same as CDI Injection.
-
14. Re: Clarification on what can be injected and where it can be?
hirowla.ian.rowlands.three.com.au Jan 6, 2010 1:48 AM (in response to hirowla.ian.rowlands.three.com.au)There's not much to show. I tag the EJB as an interceptor, write the interceptor class, and attempt to inject the logger as in the above example. The logger is null.
One difference might be that the interceptor is in a different module (a separate JAR module), but that module has its beans.xml file in it. It must have, as that module also contains the logger.