Question

Is there any way to force the use of producer method if using @Qualifier? I have a @Produces factory method but the constructor is still being called, which is an issue because I need the InjectionPoint to read parameters. Using the code below RRRRRRRR is always printed out.

@ProductTypeA
public class ProductA implements Product
{
    public String test="testA";

    private ProductA()
    {
        System.out.println("RRRRRRRRRRRRRRRRRRRR");
        this.test = "testB";
    }
    private ProductA(InjectionPoint injectionpoint)
    {
        System.out.println("TTTTTTTTTTTTTTTT");
        this.test="testC";
    }
    @Produces
    public ProductA getProductA(InjectionPoint injectionpoint)
    {
        this.test="testD";
        System.out.println("-----------------------------");
        System.out.println("injectionpoint.getAnnotated() = "+injectionpoint.getAnnotated());
        return new ProductA(injectionpoint);
    }

    @Override
    public LinkedList<Feature> getFeatures()
    {
        LinkedList<Feature> rtn = new LinkedList<Feature>();
        rtn.add( new Feature("AAA","111") );
        return rtn;
    }

    @Override
    public String toString()
    {
        return "ProductA []";
    }

}

Qualifier:

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface ProductTypeA
{
    @Nonbinding
    String testfield() default "23";
}
Was it helpful?

Solution

The Qualifier should be in the producer method:

@Produces
@ProductTypeA
public ProductA getProductA(InjectionPoint injectionpoint)
{
    this.test="testD";
    System.out.println("-----------------------------");
    System.out.println("injectionpoint.getAnnotated() = "+injectionpoint.getAnnotated());
    return new ProductA(injectionpoint);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top