I have a Maven base Java EE project: core-project
.
In this project, I use @Inject
and all things work as expected.
But after importing this project as a dependency to another project, parent-project
, the injected fields are null.
Why, after importing a jar file to other project, the injection does not work?
If your fields are empty this means that matching beans can not be resolved.
If they are in another package you need a file beans.xml
(which can be empty).
From The Java EE 6 Tutorial:
Configuring a CDI Application
An application that uses CDI must have a file named beans.xml. The file can be completely empty (it has content only in certain limited situations), but it must be present. For a web application, the beans.xml file must be in the WEB-INF directory. For EJB modules or JAR files, the beans.xml file must be in the META-INF directory.
So make sure that there is a beans.xml
in your core-project
and in parent-project
. For Maven:
from: https://stackoverflow.com/a/13057183/1838233
For EJB and JAR packaging you should place the beans.xml in src/main/resources/META-INF/. For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/
Another reason could be that your Bean is annotated with @Alternative
or @Specializes
. In that case you need to specifiy the bean as alternative in the beans.xml:
<beans ... >
<alternatives>
<class>sample.package.YourAlternativeBean</class>
</alternatives>
</beans>
See http://docs.oracle.com/javaee/6/tutorial/doc/gjsdf.html for more information on that.