How to install and use CDI on Tomcat?

Tomcat as being a barebones JSP/Servlet container doesn’t support CDI out the box. It is not correct to drop jakartaee-api.jar or javaee-api.jar in /WEB-INF/lib just to get your code to compile. The JEE API JAR contains solely the API classes, not the concrete implementation. Get rid of the whole JAR. It can cause many other … Read more

Inject list of objects in CDI (Weld)

Combining my attempts with an answer from the Weld forum: @Inject @Any private Instance<SocialNetworkService> services; Instance implements Iterable, so it is then possible to simply use the for-each loop. The @Any qualifier is needed. Another way to do this is by using the event system: create a MessageEvent (containing all the information about the message) … Read more

Why are there different bean management annotations

javax.enterprise.context.SessionScoped(JSR 346) and all other annotations under the javax.enterprise.context.* package maintain the context of CDI. CDI provides an alternative, versatile and more powerful mechanism for dependency injection, bean and general resource management within the Java EE space. It’s an alternative to JSF managed beans and it’s set to even supersede the JSF bean management mechanism … Read more

Canonical way to obtain CDI managed bean instance: BeanManager#getReference() vs Context#get()

beanManager#getReference gives you a new instance of a client proxy but the client proxy will forward method calls to the current contextual instance of a particular context. Once you obtain the proxy and keep it and the method calls will be invoked on the current instance (e.g. current request). It is also useful if the … Read more

Why use CDI in Java EE

The people that wrote CDI gave you one big object factory; they did the work for you, better than you would. It’s XML configuration or annotation driven, so you don’t have to embed everything in code. Dependency injection engines, like Spring, do a lot more than your factory. It’ll take more than one factory class … Read more

WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default

Your Customer class has to be discovered by CDI as a bean. For that you have two options: Put a bean defining annotation on it. As @Model is a stereotype it’s why it does the trick. A qualifier like @Named is not a bean defining annotation, reason why it doesn’t work Change the bean discovery … Read more

CDI: beans.xml, where do I put you?

For EJB and JAR packaging you should place the beans.xml in src/main/resources/META-INF/. For WAR packaging you should place the beans.xml in src/main/webapp/WEB-INF/. Remember that only .java files should be put in the src/main/java and src/test/java directories. Resources like .xml files should be in src/main/resources.