Code Bug Fix: CDI Bean method not called when using myfaces bundled in webapp and run on Wildfly

Original Source Link

I want to migrate my JSF Application from ManagedBean to CDI Beans.

First of all I have done a simple test to see if CDI are working, but they don’t.
Here my example, using Wildfly 10 and myfaces 2.2

beans.xml in .WEB-INF

<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">
</beans>

xhtml page:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>
        test
    </title>
</h:head>

<h:body>
    <h:outputText value="#{CDITest.hello}"/>
    <h:outputText value="test"/>
</h:body>

</html>

The backing Bean

import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import java.io.Serializable;

  @Named("CDITest")
  @SessionScoped
  public class CDITest implements Serializable{

    public String getHello(){
      return "Hello";
    }
}

The output

test

No error message (!) and no call to CDITest.getHello() method. What I’m missing?

The problem is more general.
In JSF 2.3, JSF picks up CDI via BeanManager#getELResolver. In pre JSF 2.3, the container or the CDI impl has to marry JSF and CDI.

I think you need to Declare a @FacesConfig annotated class to activate CDI in JSF 2.3

import javax.enterprise.context.ApplicationScoped;
import javax.faces.annotation.FacesConfig;

@FacesConfig
@ApplicationScoped
public class ConfigurationBean {

}

Upgrading to myfaces-2.3.6 (jsf 2.3) on Wildfly 19.0.0 solved the issue. Note that you need a @FacesConfig class as suggested by @Cristyan.

Note also that using Mojarra the issue didn’t happen and the Bean worked as expected (for both Widlfly 10 and Widlfly 19).

Tagged : / / /

Leave a Reply

Your email address will not be published. Required fields are marked *