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
- META-INF if it is a jar application
- WEB-INF if it is a web application
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();
}
}