I am working on examples of Dependency Injection in java, and most of the documents highlights that I have to put an empty beans.xml
in
So, I use war type packaging but, my application only works if I put beans.xml
in META-INF folder. I am confused a little bit about why it is working with that way ? I deploy my war file in JBOSS/WildFly container.
Here is my simple pom.xml
beans.xml lies in src/main/resources/META-INF
Also here you can see which annotations I only used for injecting the beans.
AutoService.java
public interface AutoService {
void getService();
}
BMWAutoService.java
@Named("bmwAutoService")
public class BMWAutoService implements AutoService{
@Override
public void getService() {
System.out.println("You chose BMW auto service");
}
}
AutoServiceCaller.java
@Named
public class AutoServiceCallerImp implements AutoServiceCaller{
private AutoService bmwAutoService;
@Inject
public AutoServiceCallerImp(@Named("bmwAutoService") AutoService bmwAutoService) {
this.bmwAutoService = bmwAutoService;
}
@Override
public void callAutoService() {
// get bmw's auto service
bmwAutoService.getService();
}
}
It depends where you are using annotations. If you have EJB beans in your web module and are using injections in these beans, then you will need to have beans.xml
in the META-INF
as it would be in case of pure ejb module. If you are using CDI injections in web components (servlets, filters, JSF beans) then you would have to have it in WEB-INF
. You may also need them in both places (if used from both kind of components).