将@Produces与@Qualifier一起使用

时间:2014-01-23 15:28:36

标签: java java-ee cdi jboss-weld

如果使用@Qualifier,有没有办法强制使用生产者方法?我有一个@Produces工厂方法,但仍在调用构造函数,这是一个问题,因为我需要InjectionPoint来读取参数。使用下面的代码RRRRRRRR始终打印出来。

@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
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface ProductTypeA
{
    @Nonbinding
    String testfield() default "23";
}

1 个答案:

答案 0 :(得分:3)

限定符应该在制作者方法中:

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