Java CDIBean未注入,但可以查找

Java CDIBean未注入,但可以查找,java,reflection,dependency-injection,cdi,inject,Java,Reflection,Dependency Injection,Cdi,Inject,所以,我写了一个扩展,它注册了我试图创建的bean。该bean通过CDI进行扫描,我可以使用以下方法进行扫描: MyInterface myInterface = CDI.current().select(MyInterface.class).get(); 然后我可以访问myInterface.myMethod() 但是,当我尝试使用以下方法注入bean时: @Inject @MyBean MyInterface myInterface; 它是未注入且为空的 我想要实现的是指定接口,接口定义

所以,我写了一个扩展,它注册了我试图创建的bean。该bean通过CDI进行扫描,我可以使用以下方法进行扫描:

MyInterface myInterface = CDI.current().select(MyInterface.class).get();
然后我可以访问
myInterface.myMethod()

但是,当我尝试使用以下方法注入bean时:

@Inject
@MyBean
MyInterface myInterface;
它是未注入且为空的

我想要实现的是指定接口,接口定义了一些方法,然后我的代码生成该接口的实例并返回接口类型的代理:

// defined interface
@RegisterAsMyBean
interface MyInterface {
    void myMethod();
}

// usage in code:
@Inject
@MyBean
MyInterface myInterface;
我这样声明我的bean:

public class MyExtension implements Extension {
    public void register(@Observes @WithAnnotations(RegisterAsMyBean.class) ProcessAnnotatedType<?> aType) {
        Class<?> typeDef = aType.getAnnotatedType().getJavaClass();

        if(typeDef.isInterface()) {
            proxyTypes.add(typeDef);
            aType.veto();
        }
    }
}

public class BeanCreator implements Bean<Object> {
    @Override
    public Object create(CreationalContext<Object> creationalContext) {
        // my instance building logic
    }
    // ... other overriden methods
}
公共类MyExtension实现扩展{
公共无效寄存器(@Observes@WithAnnotations(RegisterAsMyBean.class)ProcessAnnotatedType aType){
类typeDef=aType.getAnnotatedType().getJavaClass();
if(typeDef.isInterface()){
proxyTypes.add(typeDef);
aType.invoit();
}
}
}
公共类BeanCreator实现Bean{
@凌驾
公共对象创建(CreationContext CreationContext){
//我的实例构建逻辑
}
//…其他重写的方法
}

同样在META-INF/services/javax.enterprise.inject.spi.Extension中,我引用了MyExtension,问题出在bean创建生命周期中——我试图在processAnnotatedType循环中创建bean,而我应该在afterBeanDiscovery循环中创建


在将我的bean创建逻辑移动到适当的循环之后,bean被创建并正确注入。

@MyBean
是一个限定符。在
Bean
的创建过程中,它是否将
@MyBean
指定为一组限定符之一?无论如何。。。CDI应该抱怨
NoBeanDefinitionException
我没有设置任何限定符,没有(仍在学习CDI),但是CDI没有抱怨,而是使用查找的方法按照我的预期工作。这就是我发现的一些奇怪的问题-什么是
@MyBean
?您在哪里尝试使用
CDI.current()
选择您的bean,而您在那里尝试
@Inject
它?可能是您试图将其注入到非上下文实例或其他内容中。