users@glassfish.java.net

CDI not working with _at_FacesConverter

From: Dominik Dorn <dominik.dorn_at_gmail.com>
Date: Tue, 20 Apr 2010 12:55:52 +0200

Hi,

It seems to me, that CDI does not work with FacesConverters. When I'm
debugging the following class,
the injected Dao is always null.

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;


@FacesConverter(forClass = AvailableCountry.class)
public class AvailableCountryConverter
implements Converter
{
    @Inject
    AvailableCountryDao ejbFacade;

    @PostConstruct
    public void postConstruct()
    {
        System.out.println("calling postConstruct");
    }

    public Object getAsObject(FacesContext facesContext, UIComponent
component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        return ejbFacade.find(getKey(value));
    }

    Long getKey(String value) {
        Long key;
        key = Long.valueOf(value);
        return key;
    }

    String getStringKey(long value) {
        StringBuffer sb = new StringBuffer();
        sb.append(value);
        return sb.toString();
    }

    public String getAsString(FacesContext facesContext, UIComponent
component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof AvailableCountry) {
            AvailableCountry o = (AvailableCountry) object;
            return getStringKey(o.getCountry().getId());
        } else {
            throw new IllegalArgumentException("object " + object + "
is of type " + object.getClass().getName() + "; expected type: " +
AvailableCountry.class.getName());
        }
    }
}

also the postConstruct method NEVER gets called!

Is there a way, I can manually inject the Dao like
context.lookup(....) in the constructor of the converter?


Thanks,
Dominik


-- 
Dominik Dorn
http://dominikdorn.com
Tausche Deine Lernunterlagen auf http://www.studyguru.eu !