-1

While having a custom qualifier for CDI support as followed:

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface QualifiedFooBean {
}
@QualifiedFooBean
public class FooBean implements ImplFooBean {
}
public interface ImplFooBean {
}

I would like to bind FooBean #{fooBean} directly without requiring a wrapper or processor (seen from this example). The annotation "Named" (in class FooBean) seems not to work for my class layout.

My solution (without wrapper) which I'm wondering why it's not working and invoking: Target Unreachable, identifier 'fooBean' resolved to null

@Named
@QualifiedFooBean
public class FooBean implements ImplFooBean {
}

Has anyone any idea?

Ed Michel
  • 898
  • 1
  • 11
  • 23

2 Answers2

1

A wrapper is not needed. My solution is perfectly valid. It's also allowed to add Named in combination of a custom qualifier (in my case QualifiedFooBean). I had to just create an empty beans.xml file in WEB-INF folder in order to get CDI to work. Anyhow The question itself explains how custom qualifiers can work. You can also prefill beans.xml with following content:

<beans xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

</beans>

It will serve as a skeleton for future use, if you need to configure more fancy stuff with CDI.

Ed Michel
  • 898
  • 1
  • 11
  • 23
  • CDI wasn't activated @maglebolia so why did you said the wrapper solution was working for you. It's better to give effective information when asking help. – Antoine Sabot-Durand Jul 25 '13 at 12:06
  • This was due to my confidence of the seen examples on the net always with a wrapper, I just took it for granted that it'll work. But I get the point of actually explaning my situation and not making assumptions. Thank you anyway for your time. – Ed Michel Jul 25 '13 at 13:47
-1

Adding @Named to your bean should work : it works for me.

In Fact @Named is a qualifier, when JSF resolve the Bean for displaying it does a lokup based on @Named qualifier. In CDI a bean is found if the lookup side (i.e. Injection point) ask for a subset of its Qualifier.

For instance a bean qualified like that :

@QualifiedFooBean
@SecondQualifier
public class FooBean {}

Will be found (if there is no ambiguous resolution) by

@Inject
@SecondQualifier
FooBean bean;

or even :

@Inject
FooBean bean;

But not by

@Inject
@SecondQualifier
@ThirdQualifier
FooBean bean;

So you can add the @Named qualifier and let CDI resolution engine do its job.

Antoine Sabot-Durand
  • 4,875
  • 17
  • 33
  • I've updated my question, to reflect my issue better. @Antoine: I had ambiguous warnings before creating the custom qualifier. They are gone... combination of Named and Default works but it seems to fail when it's actually a costum qualifier Named + QualifiedFooBean – Ed Michel Jul 25 '13 at 08:38
  • Do you have a stack trace? Have you tried to put your bean in RequestScope (like that it's dependent and is created each time requested) – Antoine Sabot-Durand Jul 25 '13 at 08:49