Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting Resources Into UIComponent (aka does CDI work here?)

I am writing a (composite) component that needs to interact with my DAO. Here is how the Java part is declared:

@FacesComponent(value="selectLocation")
public class SelectLocation extends UINamingContainer {

To get the DAO object, I tried the CDI annotation:

    @Inject private LocationControl lc;

And that didn't work so I tried the Faces annotation:

    @ManagedProperty (value = "@{locationControl}") private LocationControl lc;

Both cases nothing happens -- the property lc ends up as null after the constructor finishes.

I use CDI in all my backing beans and it all works. This would be using Weld inside GlassFish 3.1.1. Any suggestions on how to get the resource?

like image 448
AlanObject Avatar asked Aug 23 '11 20:08

AlanObject


2 Answers

I have a work-around for now, which is to basically put in the boiler-plate code that CDI et. al. is supposed to do away with. I now have this method:

public LocationControl getLocationControl() {
    if (lc != null) return lc;
    FacesContext fc = getFacesContext();
    Object obj = fc.getApplication().evaluateExpressionGet(fc, "#{locationControl}", LocationControl.class);
    if (obj instanceof LocationControl) lc = (LocationControl) obj;
    return lc;
}

I would like to know if anyone has a better solution.

like image 148
AlanObject Avatar answered Nov 15 '22 10:11

AlanObject


I don't know if it also works for components, but with CDI + MyFaces CODI you have @Advanced to mark e.g. Phase-Listeners which should be able to use @Inject. If it doesn't work, you could create a feature request in their JIRA. They are pretty fast and there are frequent releases.

Or you use: MyBean myBean = BeanManagerProvider.getInstance().getContextualReference(MyBean.class); manually.

like image 34
Dar Whi Avatar answered Nov 15 '22 09:11

Dar Whi