Dependency injection is not working
daviddev Jul 7, 2009 4:59 AMHi guy,
I start to be a bit confuse with the dependency injection. I really want to understand how this works.
I try several thinkg and sometime it is working sometimes not.
Basicaly I have an application (i.e. MM) which consists of a set of Message driven beans, beans, and entities that I want to used in another application (i.e. CMS) as a kind of sub-component.
So I do include MM in CMS (the ejb-jar.xml, and persistence.xml file are used to "configurate" MM).
For some reason MM need to know the application (e.g. CMS) that is using it so I used the ejb-jar.xml file and a <env-entry> specification in each of my message driven and other beans to specify this information.
Each bean (message driven or others) in MM has a property (i.e. applicationName) annotated with @Resource and a get and set method to access it.
It seems that the injection of the resource my approach is working only for message-driven bean but not the other beans and I do not understand why.
MM is deploy within CMS on JBOSS with a .ear file which as this structure:
CMS.EAR |--META-INF | |--application.xml |--CMC.jar (message driven and persitence beans) | |--META-INF | |--ejb-jar.xml |--CMejb.jar (entities classes) | |--META-INF | |--persistence.xml |--MMejb.jar (MM message driven beans) |--MSMejb.jar (MM entity classes and persistence beans) |--CMSweb.war (CMS servlet...) |--MMweb.war (MM Servlet ...)
the application.xml in CMS.ear is:
<application> <display-name>CMS</display-name> <module> <ejb>lib/cmsconfiguratorclient.jar</ejb> </module> <module> <ejb>CMC.jar</ejb> </module> <module> <ejb>CMejb.jar</ejb> </module> <module> <web> <web-uri>CMSweb.war</web-uri> <context-root>CARDWeb</context-root> </web> </module> <!-- MM --> <module> <ejb>MMweb.war</ejb> </module> <module> <ejb>MMejb.jar</ejb> </module> <module> <ejb>MSMejb.jar</ejb> </module> </application>
the ejb-jar.xml in CMC is
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"> <display-name>CMS</display-name> <enterprise-beans> <message-driven > <display-name>MessagerReceiver</display-name> <ejb-name>MessageReceiver</ejb-name> <ejb-class>com.messaging.messagemanager.receiving.MessageReceiver</ejb-class> <transaction-type>Container</transaction-type> <activation-config> <activation-config-property> <activation-config-property-name>destinationType</activation-config-property-name> <activation-config-property-value>javax.jms.Queue</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>destination</activation-config-property-name> <activation-config-property-value>queue/CMS</activation-config-property-value> </activation-config-property> </activation-config> <env-entry> <description>The name of the application</description> <env-entry-name>com.messaging.messagemanager.receiving.MessageReceiver/applicationName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>CMS</env-entry-value> </env-entry> </message-driven> <session> <ejb-name>IncomingMessageHandler</ejb-name> <ejb-class>com.ecebs.multefile.messaging.messagemanager.receiving.IncomingMessageHandler</ejb-class> <env-entry> <description>The name of the application</description> <env-entry-name>com.messaging.messagemanager.receiving.IncomingMessageHandler/applicationName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>CMS</env-entry-value> </env-entry> </session> <session> <ejb-name>MessageStoreManagerBean</ejb-name> <ejb-class>com.messaging.messagestoremanager.MessageStoreManagerBean</ejb-class> <env-entry> <description>The name of the application</description> <env-entry-name>com.messaging.messagestoremanager.MessageStoreManagerBean/applicationName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>CMS</env-entry-value> </env-entry> <persistence-context-ref> <persistence-context-ref-name>messagemanager_em</persistence-context-ref-name> <persistence-unit-name>messagemanager</persistence-unit-name> </persistence-context-ref> </session> </enterprise-beans> </ejb-jar>
all beans from MM are defined here with the same <env-entry>. here I put only few of them to make it short.
when I debug my application and send a mock message to the "MessagerReceiver" the "applicationName" is set with CMS so the dependency injection did work.
When i simulate I use the IncomingMessageHandler bean, the applicationName is not set with the value specified in the ejb-jar.xml (i.e. CMS).
here the code of the incomingMessageHandler bean and MessageReceiver message driven bean:
@Stateless public class IncomingMessageHandler implements IIncomingMessageHandler{ //... @Resource private String applicationName; public String getApplicationName() { return applicationName; } public void setApplicationName(String applicationName) { this.applicationName = applicationName; } //... } public class MessageReceiver implements MessageListener{ //... @Resource private String applicationName; public String getApplicationName() { return applicationName; } public void setApplicationName(String applicationName) { this.applicationName = applicationName; } public void onMessage(Message message) { //... } //...
a little help and explaination will be appreciated to understand what is wrong in my code/approach.
Any good references/internet link that could help me to understand the structure of an ear file or how to use ejb-jar.xml and persistence.xml in ear are also welcome.
Thanks a lot