3

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();
    }
}
quartaela
  • 2,579
  • 16
  • 63
  • 99

1 Answers1

3

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).

Gas
  • 17,601
  • 4
  • 46
  • 93
  • Thanks for your answer. I have only used `@Named` and `@Inject` annotations to inject the beans. So, does it mean that I am using EJB beans ? And I added very simple example of code I used – quartaela Dec 30 '14 at 00:54
  • @quartaela No, it looks like you are using only web beans. So after packaging your beans.xml should be in `WEB-INF`. Try to put your beans.xml into `src\main\webapp\WEB-INF` folder, not `src/main/resources/META-INF`. – Gas Dec 30 '14 at 11:14
  • That works! Can you tell me difference between `src\main\webapp` and `src\main\resources` ? I was trying to put `WEB-INF` in resources folder as I do for `META-INF` – quartaela Dec 30 '14 at 20:57
  • 1
    @quartaela This is how [Maven archetype](http://maven.apache.org/guides/introduction/introduction-to-archetypes.html) works. The `resources` folder is a root folder for resources :) and files placed there are copied to the compiled classes folder, so `WEB-INF/classes` in case of war packaging and for `beans.xml` it is not correct location. From the other hand `webapp` is a root for web application files, like htmls, jsps, images, etc. and is also used to store any descriptors in the `WEB-INF` subfolder (like web.xml) and other config files which should be placed in `WEB-INF` after packaging. – Gas Dec 30 '14 at 22:43