ApplicationScoped bean not injected in JSF 2.3 FacesConverter

1.2k Views Asked by At

I have following FacesConverter:

@FacesConverter(forClass = Onderwerp.class, managed = true)
public class OnderwerpConverter implements Converter<Onderwerp> {

@Inject
private Web web;

@Override
public Onderwerp getAsObject(FacesContext context, UIComponent component, String value) {

    log.trace("Converting to object from string: " + value);

    return web.getAllActiveOnderwerpen().stream().filter(o -> o.getId().equals(Long.parseLong(value))).findFirst().get();
}


@Override
public String getAsString(FacesContext context, UIComponent component, Onderwerp onderwerp) {

    log.trace("Converting to string from object: " + onderwerp);

    return onderwerp.getId().toString();
}

}

The referenced CDI bean is:

@Named
@ApplicationScoped
public class Web { ... }

Faces-config.xml is:

<?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">

Now whatever kind of bean I inject via @Inject it is always null. It seems the injection feature in 2.3 is not working (yet), or I am missing something :)

I'm using Mojarra 2.3.0 on EAP 7.0. Also tested without success using 2.3.3 on EAP 7.0 and 7.1.

My current workaround is replacing the code where I need the injected CDI bean like this:

return CDI.current().select(Web.class).get().getAllActiveOnderwerpen().stream().filter(o -> o.getId().equals(Long.parseLong(value))).findFirst().get();

This works fine, but is kinda ugly of course :)

Anyone has experienced this behavior?

1

There are 1 best solutions below

7
AudioBubble On

I had the same issue and I foud the solution.

You must create a config bean indicating JSF version:

import javax.faces.annotation.FacesConfig;
import javax.faces.annotation.FacesConfig.Version;


@FacesConfig(
        // Activates CDI build-in beans
        version=Version.JSF_2_3 
        )
public class ConfigBean {

}