Injection is not an automagic happening. The AS takes the servlet class and does all the injection for it (forcing it into the fields), that is why the "new" operator doesn't work with injection either.
If you want to separate the stuff, you could use CDI by adding an empty beans.xml, move logic to the JMSUtility and do an @Inject JMSUtility util; in the servlet class.
1 of 1 people found this helpful
Alternatively, you could add an @Stateless annotation to JMSUtility and inject it into your servlet with @Inject or @EJB.
JMS is a transactional resource, so this might be considered more appropriate than straight CDI.
Either way, the thing to remember is that you only get injection when the bean lifecycle is managed by the container.
True, and no need bringing in new technologies if you're not using it for anything else and it can be done with the existing stack. Although I don't think it matters in this case since the methods being called would still be the transactional ones and not the helper methods directly.
Thank you very much for the kind replies (both correct I guess so I had to pick up just the first reply as the correct one :-) )
Thats very clear, so it looks pretty the same as Spring container (where injections will not work if you are using the new operator for creating your beans).
Out of curiosity, am I allowed to install as module the POJO class (in the modules folder), with a beans.xml file in the JAR file, and then @Inject the JMSUtility class in my applications ?
Thanks & have a nice day
There is a high change the CDI approach wouldn't work in that case since the visiblity rules of the EE deployment is followed, per spec, and the JBoss module is alien to the WAR archive (even if class visiblity is established to the JBoss Modules thanks to the AS)
Thanks. By the way I have just tried installing the Utility class as a module (without injections) but I get an error:
JBAS011843: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "org.jboss.as.connector:main" from local module loader @d9660d (roots: C:\jboss-as-7.1.1.Final\modules)
I will open another thread for this issue.