I'm making two beans say ABean and BBean. I want to inject BBean into ABean but this is causing null pointer errors, likely because the bean has yet to be instantiated. How can I inject beans in beans as such:
@Singleton
public class ABean {
@Inject
BBean bean;
....
}
I'm using java ee 7 with wildfly server. Both beans are singletons so BBean is also declared as:
public class BBean {
@PostConstruct
public void startup() {
..
}
..
}
With out the dependency, I'm able to create both beans as I do have the necessary META-INF folder and beans.xml file with in it. I'm however coming to the conclusion that this might be bad practice/ anti-pattern. Anyway I'm not using this approach anymore.
Perhaps you forgot to add the beans.xml file, in order to enable CDI in your application. This is what the Java EE 6 Tutorial says http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html:
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.