@Inject to pass params to a CDI @Named bean via URL

This works only with the in JSF 2.3 introduced javax.faces.annotation.ManagedProperty. @Inject @ManagedProperty(“#{param.id}”) private String id; The now deprecated javax.faces.bean.ManagedProperty annotation works only in JSF @ManagedBean classes. I.e. in instances which are managed by JSF. It does not work in instances which are managed by CDI @Named. Further, you’ve made another mistake: you’re trying to prepare … 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.

How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1

If you can upgrade to JSF 2.2, immediately do it. It offers a native @ViewScoped annotation for CDI. import javax.faces.view.ViewScoped; import javax.inject.Named; @Named @ViewScoped public class Bean implements Serializable { // … } Alternatively, install OmniFaces which brings its own CDI compatible @ViewScoped, including a working @PreDestroy (which is broken on JSF @ViewScoped). import javax.inject.Named; … Read more

Should I use @EJB or @Inject

The @EJB is used to inject EJB’s only and is available for quite some time now. @Inject can inject any managed bean and is a part of the new CDI specification (since Java EE 6). In simple cases you can simply change @EJB to @Inject. In more advanced cases (e.g. when you heavily depend on … 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

Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean

First of all let me do some clarifications: Managed bean definition : generally a managed bean is an object that its life cycle (construction, destruction, etc) is managed by a container. In Java ee we have many containers that manage life cycle of their objects, like JSF container, EJB container, CDI container, Servlet container, etc. … Read more

How to install and use CDI on Tomcat?

Tomcat as being a barebones JSP/Servlet container doesn’t support CDI out the box. How exactly did you install CDI? Did you really drop jakartaee-api.jar or javaee-api.jar in /WEB-INF/lib just to get your code to compile? Oh please no, this is not the right way. The JEE API JAR contains solely the API classes, not the … Read more