How to inject a CDI Bean in a ManagedBean? - jsf

I want to inject a CDI Bean in a ManagedBean either with the annotation #Inject or #Produce. The CDI Bean which I use is:
#Named
#Startup
#ApplicationScoped
public class BaseBean {
private List<String> custs;
public List<String> getCusts() {
return custs;
}
public void setCusts(List<String> emps) {
this.custs = emps;
}
public BaseBean(){
}
#PostConstruct
void init() {
custs = new ArrayList<String>();
custs.add("Cust1");
custs.add("Cust3");
custs.add("Cust2");
custs.add("Cust4");
}
}
The ManagedBean, in which I want to inject the CDI Bean is:
#SessionScoped
#ManagedBean
public class Hello implements Serializable {
#Inject
private BaseBean dBean;
private static final long serialVersionUID = 1L;
private List<String> customers;
private List<String> customersSelect;
public Hello() {
}
#PostConstruct
void init() {
// dBean = new BaseBean();
customers = dBean.getCusts();
}
public List<String> getCustomers() {
return customers;
}
public List<String> getCustomersSelect() {
return customersSelect;
}
public void setCustomersSelect(List<String> customersSelect) {
this.customersSelect = customersSelect;
}
}
In the init function however, it throws NullPointerException. If I use the annotation #Produces instead of #Inject, the result is the same: NullPointerException. Is anything wrong with the CDI Bean (improper annotations)? Do I try to inject it with a wrong way? Does my code lack of something? How can I get it work? Here is the JSF code:
<h:form id ="f">
<h:selectManyCheckbox layout="pageDirection" border="1" value="#{hello.customersSelect}">
<f:selectItems value="#{hello.customers}"></f:selectItems>
</h:selectManyCheckbox><br />
<h:commandButton action="response.xhtml" value="Click me" />
</h:form>
PS: If I use a Stateless Bean as BaseBean and I inject it with the annotation #EJB, it works with no problem.
UPDATE: I have tried it with the annotations #SessionScoped (javax.enterprise.context.SessionScoped) and #Named on the Hello class. Although I do not receive a NullPointerException, the h:selectManyCheckbox is empty. moreover, it strikes me, that when I add the beans.xml file under META-INF folder, I receive a StartException, although the file is there supposed to be. I think my application lacks the proper configuration to be capable of Dependency Injection. What is likely to need additional configuration?
UPDATE 2: This error appears when I add the beans.xml file in the WEB-INF folder. The beans.xml file is empty, it contains only the line :
<?xml version="1.0" encoding="UTF-8"?>
The error is:
Services which failed to start: service jboss.deployment.unit."JSF1.war".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."JSF1.war".PARSE: Failed to process phase PARSE of deployment "JSF1.war"
12:51:11,482 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"JSF1.war\".PARSE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"JSF1.war\".PARSE: Failed to process phase PARSE of deployment \"JSF1.war\""}}}}

What #patlov is suggesting will work if you use #Named on your CDI beans. However, if you're working in an environment that supports CDI, do not use #ManagedBean. Instead, use CDI all the way. See this answer and I'm sure you could find numerous other ones that strongly advise against what you're trying to do.
Just switch from javax.faces.bean.SessionScoped to javax.enterprise.context.SessionScoped and everything will magically work. What you may run into is the absense of #ViewScoped from CDI however, in which case use something like JBoss Seam or Apache Deltaspike that implement it for you. As an added benefit, they will also automatically replace all of the JSF scopes with CDI scopes automatically if you already have existing code written for JSF.
Update:
This should be the content of your beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

Make sure you have enabled CDI by putting a WEB-INF/beans.xml file in your application.

If you are using #ManagedBean use #ManagedProperty to inject properties:
#ManagedProperty(value = "#{baseBean}")
private BaseBean dBean;
// getter and setter

Related

Is it possible to inject a bean to phaselistener when using JSF 2.3, tomcat with weld cdi implementation?

I want to inject a #SessionScoped and a #RequestScoped beans into my PhaseListener implementation, but I got NullPointerException.
I use tomcat with weld implementation to achieve CDI. I started to migrate JSF 2.2 to 2.3, so I changed to CDI from FacesContext.
Well I replaced #ManagedBean to #Named and any other things to must do during migration like:
- add beans XML to every modules
- add BeanManager to context XML
- delete bean declarations from faces-config.xml
- add SPI BeanManager as resource-env-ref to web.xml
How can I inject any bean to PhaseListener implementations?
#Named
#SessionScoped
public class MyHandler implements Serializable {
..}
#Named
#RequestScoped
public class MyController extends MyParentController<Example> {
..}
public class MyPhaseListener implements PhaseListener {
private MyHandler myHandler;
private MyController myController;
#Inject
public void setMyHandler(MyHandler myHandler) {
this.myHandler= myHandler;
}
#Inject
public void setMyController (MyController myController) {
this.myController= myController;
}
...
public void afterPhase(PhaseEvent event) {
myHandler.method()
}
myHandler injected bean is null in afterPhase method.
I put CDI configuration file beans.xml into folder META-INF instead of WEB-INF
In beans.xml I had to change bean-discovery-mode from "annotated" to "all".
I forgot to add a class with #FacesConfig(version = Version.JSF_2_3) annotation( to enable EL resolution for CDI beans).
Also forgot to change faces-config.xml version to 2.3
Fortunately I solved the problem... A few things I had to do:
I put beans.xml into META-INF/, but beans.xml has to be under
WEB-INF.
In beans.xml I had to change bean-discovery-mode from "annotated" to
"all".
I forgot to add a class with #FacesConfig(version = Version.JSF_2_3)
annotation( to enable EL resolution for CDI beans).
Also forgot to change faces-config.xml version to 2.3.

Instance null after injecting #SessionScoped CDI bean into a JSF #ManagedBean

I have :
- Repository Class:
#SessionScoped
public class EmployeeRepository {
#PersistenceContext
EntityManager entityManager;
public List<Employee> getEmployees(){
TypedQuery<Employee> qu = entityManager.createQuery("select * from Employee", Employee.class);
List<Employee> emp2 = qu.getResultList();
return emp2;
}
}
and
Managed Bean:
#ManagedBean(name = "helloWorldBean")
public class HelloWorldBean {
#Inject
private EmployeeRepository employeerepo;
public String getMsg() {
return "Hallo";
}
public String getEmployees() {
return String.valueOf(employeerepo.getEmployees().size());
}
}
and a JSF Page:
<h:head>
<title>JavaCodeGeeks</title>
</h:head>
<h:body>
- Message : <h:outputText value="#{helloWorldBean.msg}" />
- Employee count : <h:outputText value="#{helloWorldBean.employees}" />
</h:body>
</html>
I have an beans.xml in my META-INF Folder (src\META-INF) without special configurations:
<?xml version="1.0"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all" version="1.1">
</beans>
Problem:
Page throws nullpointer exception, because the EmployeeRepository is not injected in the HelloWorldBean.
What to do to be able to inject Instances of classes in my case ?
As you are using CDI, you should not be using #ManagedBean (which is a JSF annotation). While I have actually seen this working, most implementations will not allow you to inject a CDI bean into a classic JSF bean.
To allow for CDI injection in JSF beans, the CDI specification allows you to define JSF backing beans by specifying the #Named annotation in combination with a scope (#RequestScoped, #javax.faces.view.ViewScoped, #SessionScoped and #ApplicationScoped).
So in summary, the following piece of code should solve your problem,
#Named
#RequestScoped
public class HelloWorldBean {
#Inject
private EmployeeRepository employeerepo;
public String getMsg() {
return "Hallo";
}
public String getEmployees() {
return String.valueOf(employeerepo.getEmployees().size());
}
}
You can also read up further on the topic via some old Q/A's here on the site,
How to inject a CDI Bean in a ManagedBean?
#ManagedProperty does not work in a CDI managed bean
CDI beans injection

Interceptors don't work with JSF managed beans?

I am decide to try interceptors My first interceptor binding annotation is
#Inherited
#InterceptorBinding
#Target({TYPE})
#Retention(RUNTIME)
public #interface WithLog {
// No parameters required
}
And interceptor class is
#Interceptor
#WithLog
public class LogInterceptor {
#AroundInvoke
private Object logMethod(InvocationContext context) throws Exception {
System.out.println("Method " + context.getMethod().getName() +
" of class " + context.getTarget().getClass().getName() + " was called.");
return context.proceed();
}
#PostConstruct
private void construct(InvocationContext context) {
System.out.println("#Postconstruct of " +
context.getMethod().getDeclaringClass().getName() + " started.");
}
}
So, I want to add simple logging for JSF managed bean:
#ManagedBean(name = "departmentRootMB")
#ViewScoped
#WithLog
public class DepartmentRootMB implements Serializable {
long serialVersionUID = 0L;
// . . . properties, methods
}
I read, that to enable interceptors I need to create beans.xml. I created one in the WEB-INF directory:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
<interceptors>
<class>ru.edu.pgtk.weducation.interceptors.LogInterceptor</class>
</interceptors>
</beans>
I rebuild project and no effect. Where is the mistake? What I did wrong? I use glassfish 4.1 with it standard components (WELD, EclipseLink, JSF 2.2.7)
Thanks for your time and best regards.
Interceptors don't work with JSF managed beans?
Correct.
Replace JSF bean management facility by CDI bean management facility.
In other words, replace #ManagedBean and friends by #Named and friends. The JSF bean management facility is on the schedule to be deprecated in favor of CDI in a future Java EE version anyway. This is a good opportunity to migrate now you can.
See also:
Backing beans (#ManagedBean) or CDI Beans (#Named)?

#ManagedBean(name="foo") not available by its name in EL #{foo}

I'm trying to use an application scoped bean in JSF2, but for some reason it is always null in my request scoped bean. Here's the code I'm using:
The application scoped bean:
#ManagedBean(eager=true, name="applicationTracking")
#ApplicationScoped
public class ApplicationTracking implements Serializable {
private static final long serialVersionUID = 4536466449079922778L;
public ApplicationTracking() {
System.out.println("ApplicationTracking constructed");
}
}
The request scoped bean:
#ManagedBean
#RequestScoped
public class SearchResults implements Serializable {
private static final long serialVersionUID = 4331629908101406406L;
#ManagedProperty("#{applicationTracking}")
private ApplicationTracking appTracking;
public ApplicationTracking getAppTracking() {
return appTracking;
}
public void setAppTracking(ApplicationTracking appTrack) {
this.appTracking = appTrack;
}
#PostConstruct
public void init() {
System.out.println("SearchResults.init CALLED, appTracking = " + appTracking);
}
}
According to everything I'm seeing in the forums this should work without any other configurations. When I start the server (Tomcat) I'm seeing the ApplicationTracking constructor and init methods getting called.
But in my SearchResults component the printout in the PostConstruct is always null:
SearchResults.init CALLED, appTracking = null
What am I missing?
Provided that you imported those annotations from the right package javax.faces.bean.*, then this problem will happen if you re-registered the very same managed bean class in faces-config.xml on a different managed bean name. Get rid of that faces-config.xml entry. That's the JSF 1.x style of registering managed beans. You don't need it in JSF 2.x. When you do so anyway, then it will override any annotation based registration on the managed bean class, causing them to be ineffective.
Make sure you don't read JSF 1.x targeted resources while learning and working with JSF 2.x. Many things are done differently in JSF 2.x.

Cannot inject CDI #SessionScoped in HttpSessionListener

I have a stateful, session scoped (CDI) EJB that holds the info about the user session.
#Stateful
#SessionScoped
public class GestorSesion implements IGestorSesionLocal, Serializable {
private final static long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(GestorSesion.class.getName());
#PostConstruct
private void init() {
log.info("Configurando información de usuario");
log.info("****************************************************************************");
}
#Override
public void cerrarSesion() {
}
#Override
public ISessionInfo getSesionInfo() {
return null;
}
}
Now, I want to call cerrarSesion() (closeSession()) from an HttpSessionListener
public class GestorSesionWeb implements HttpSessionListener {
private static final Logger log = Logger.getLogger(GestorSesionWeb.class.getName());
#Inject
private Instance<IGestorSesionLocal> gestorSesion;
#Override
public void sessionCreated(HttpSessionEvent se) {
if (log.isLoggable(Level.FINE)) {
log.fine("Iniciada sesión web");
}
gestorSesion.get().getSesionInfo();
}
#Override
public void sessionDestroyed(HttpSessionEvent se) {
if (log.isLoggable(Level.FINE)) {
log.fine("Limpiando sesión al salir");
}
try {
this.gestorSesion.get().cerrarSesion();
if (log.isLoggable(Level.FINE)) {
log.fine("Sesión limpiada sin problemas");
}
} catch (Exception e) {
log.log(Level.WARNING, "Excepción limpiando sesión", e);
}
}
}
And I access the EJB from the webapp, directly (injecting using #EJB) into the beans that I use for JSF (they are also CDI managed beans).
The issue that I face is that it seems that the HttpSessionListener seems to be in a different "session scope" than the JSF beans. Two instances of GestorSession are created; one instantiated from the JSF and other instantiated from the HttpSessionListener.
I have tried injecting the bean via #Inject Instance<IGestorSesionLocal>, #Inject IGestorSesionLocal and BeanManager, with identical results.
This bug report suggests that it should to work correctly (the bug is solved for my version), but I still cannot get around it. I have looked around, but what I find are Q&A relating to JSF managed beans (yes, I could try to wrap a reference to the EJB in a JSF managed bean, but I would like to try it "correctly" first).
Working with WilFly 8.1.0 and JDK 7
Any ideas?
I think your pb comes from here:
And I access the EJB from the webapp, directly (injecting using
#EJB) into the JSF beans.
When use CDI with EJB (by putting a #Sessionscoped on it for instance) you got an CDI bean that also has an EJB nature but not the other way around. In other word CDI is aware of the EJB nature but EJB is not aware of CDI.
So if you want to inject an EJB as a CDI bean in your code use #Inject and not #EJB. From Java EE 6 the admitted good practice is to always use #Inject except for EJB remote.
Also be sure to only use CDI managed bean and not a mix of CDI and JSF managed bean.

Resources