My web app only works with META-INF instead of WEB-INF including beans.xml. What for?

I am working on Dependency Injection examples in java and most docs emphasize that I should put empty beans.xml

in

  • META-INF if it's a jar app
  • WEB-INF if it is a web application.

So, I am using war type packaging, but my application only works if I put beans.xml

in the META-INF folder. I am a little confused about why it works with this path? I am deploying my war file in a JBOSS / WildFly container.

Here is my simple pom.xml

beans.xml is in src/main/resources/META-INF

Also here you can see which annotations I only used to inject 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 auto service
        bmwAutoService.getService();
    }
}

      

+3


source to share


1 answer


It depends on where you are using annotations. If you have EJB beans in your web module and you are injecting those beans, then you need to have beans.xml

in META-INF

as you would in the case of a pure ejb module. If you are using CDI injection in web components (servlets, filters, JSF beans) you must have it in WEB-INF

. They may also need them in both places (if used from both components).



+3


source







All Articles