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?

- 6,393
- 1
- 14
- 30

- 43
- 9
-
It means that CDI is not activated in your parent project. May be `beans.xml` is missing? – Rouliboy May 13 '17 at 10:45
-
Looks like the parent project does not have CDI enabled. Cannot really help further as you haven't shared your deployment structure. – Siliarus May 15 '17 at 06:06
1 Answers
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.

- 3,267
- 31
- 41
-
sometimes [empty is wrongly treated as invalid](https://github.com/jQAssistant/jqassistant/issues/354), so `
` will do – Andreas Covidiot Apr 02 '21 at 19:56