CDI程序化查找异常(InjectionPoint)

时间:2014-12-10 09:08:38

标签: java-ee cdi

我正面临着TomEE服务器上的错误。 CDI动态查找无法按计划运行。

特别是当我想查找的bean是由一个工厂(@Produces)以注入作为参数生成的。

以下是我的问题的一个简单示例。

限定符@RandomNumber

@Qualifier
@Retention(RUNTIME)
@Target({ METHOD, FIELD, PARAMETER, TYPE })
public @interface RandomNumber {

@Nonbinding
int min() default 0;

@Nonbinding
int max() default 10; 
}

他的Literal RandomNumerLiteral

public class RandomNumberLiteral extends AnnotationLiteral<RandomNumber> implements RandomNumber {

private final int   min;

private final int   max;

public RandomNumberLiteral(final int min, final int max) {

    this.min = min;
    this.max = max;
}

@Override
public int max() {

    return this.max;
}

@Override
public int min() {

    return this.min;
}

}

工厂方法(POJO)

@Produces
@RandomNumber
public Integer producesNumber(final InjectionPoint ip) {

final int min = ip.getAnnotated().getAnnotation(RandomNumber.class).min();
final int max = ip.getAnnotated().getAnnotation(RandomNumber.class).max();

return (new Random().nextInt((max - min) + 1) + min);
}

测试类(动态查找的选择结果既不含糊也不满足)

public class Test {

@Inject
@RandomNumber(min = 20, max = 50)
private Integer                         random;

@Inject
@Any
private Instance<Integer>               randomInstance;

public void test() {
    System.out.println("Random1: " + this.random);
    System.out.println("Random2: " + this.randomInstance.select(new RandomNumberLiteral(20,50)).get());
}
}

第一个声明工作正常但是第二个声明我得到了例外:

javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: 
java.util.EmptyStackException
at org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(BaseEjbProxyHandler.java:363)
at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:283)

blablabla

Caused by: java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at org.apache.webbeans.component.InjectionPointBean.createInstance(InjectionPointBean.java:83)
at org.apache.webbeans.component.InjectionPointBean.createInstance(InjectionPointBean.java:28)
at org.apache.webbeans.component.AbstractOwbBean.create(AbstractOwbBean.java:186)
at org.apache.webbeans.context.DependentContext.getInstance(DependentContext.java:70)
at org.apache.webbeans.context.AbstractContext.get(AbstractContext.java:132)
at org.apache.webbeans.container.BeanManagerImpl.getReference(BeanManagerImpl.java:872)
at org.apache.webbeans.inject.InjectableMethods.doInjection(InjectableMethods.java:96)
at        org.apache.webbeans.component.ProducerMethodBean.createDefaultInstance(ProducerMethodBean.java:161)
at org.apache.webbeans.component.ProducerMethodBean.createInstance(ProducerMethodBean.java:123)
at org.apache.webbeans.component.AbstractOwbBean.createNewInstance(AbstractOwbBean.java:233)
at org.apache.webbeans.portable.creation.AbstractProducer.produce(AbstractProducer.java:77)
at org.apache.webbeans.component.InjectionTargetWrapper.produce(InjectionTargetWrapper.java:133)
at org.apache.webbeans.component.AbstractOwbBean.create(AbstractOwbBean.java:180)
at org.apache.webbeans.context.DependentContext.getInstance(DependentContext.java:70)
at org.apache.webbeans.context.AbstractContext.get(AbstractContext.java:132)
at org.apache.webbeans.container.BeanManagerImpl.getReference(BeanManagerImpl.java:872)
at org.apache.webbeans.inject.instance.InstanceImpl.get(InstanceImpl.java:126)

我需要帮助! 谢谢大家 !

0 个答案:

没有答案