Stephan
Stephan

Reputation: 736

@Inject not working in @FacesConverter after upgrade to JSF 2.3

I migrated a Primefaces 6 application to JBoss EAP 7.4 and upgraded to Primefaces 10 and JSF2.2 to JSF2.3. After this migration, the @Inject we had in the FacesConverter is not being initialized and thus returning null. This question might be duplicate (https://stackoverflow.com/a/47447140/1918516), the proposed answers did not resolve my issue.

Below my current project structure.

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
            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/web-facesconfig_2_3.xsd"
            version="2.3">
    ...
</faces-config>

MyConverter.java

@FacesConverter(value = "MyConverter", managed = true)
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class MyConverter implements Converter {
...
   @Inject
   private MyBean myBean;
}

MyBean.java

@Stateless
@Named
public class MyBean{
...
}

Jsf23Activator.java

@ApplicationScoped
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class Jsf23Activator {

}

web.xml

<web-app 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/web-app_4_0.xsd"
             version="4.0">
    ...

    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN</param-name>
        <param-value>true</param-value>
    </context-param>
</web-app>

I have not created a beans.xml as it is not required with newer versions as far as i know. Also when creating one, i got the common error:

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type MyDao with qualifiers @Default

  1. I got it work for the converters by injecting with CDI.current().select(MyBean.class).get(); but for me this is just a workaround

  2. Another approach would be to make a @Named out of the MyConverter and use following in my jsf, but that would be ideal for JSF 2.2 not 2.3 :

    <h:inputSomething ...> <f:converter binding="#{myConverter}" /> </h:inputSomething>

Upvotes: 2

Views: 1168

Answers (2)

Stephan
Stephan

Reputation: 736

The missing part of the implementation, was the beans.xml file which I intentionally did not use as starting from version 1.1 it is not mandatory, as mentioned in my question as well.

It looks that in order to inject inside a FacesConverter the creation of the following beans.xml file is needed as well.

<?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_2_0.xsd"
       bean-discovery-mode="all"
       version="2.0">
</beans>

Upvotes: 2

Melloware
Melloware

Reputation: 12039

I use Jboss 7.4 EAP and my @Inject works fine inside my converters. Here is my example converter. Then again I DO have a beans.xml in my WEB-INF/ folder.

@FacesConverter(forClass = AnnulmentReason.class)
public class AnnulmentReasonConverter extends AbstractEntityConverter<AnnulmentReason, Long> {

   /** ApplicationScoped cache */
   @Inject
   EntityCache<AnnulmentReason, Long> cache;

   @Override
   protected AnnulmentReason getObjectById(final Long id) {
      final Map<Long, AnnulmentReason> map = cache.getMap();
      final AnnulmentReason ar = map.get(id);
      return ar;
   }

   @Override
   protected Long getObjectId(final AnnulmentReason entity) {
      return entity.getId();
   }

}

Upvotes: 1

Related Questions