CDI注入现有对象

问题描述:

假设我有以下课程:

public class MyRequestPayload implements RequestPayload {

    protected MyRequestPayload() {}

    @Override
    public ResponsePayload process() {
        String result = someService.doSomething(foo, bar);
        return new MyResponsePayload(result);
    }

    public final String foo;

    public final Integer bar;

    @Inject
    private SomeService someService;
}

我可以调用哪些CDI服务来处理所有 @Inject 此类实例上的注释,注入当前可用的所有匹配服务?对于不是单例且不是由CDI创建的对象,需要这样做。在上面的假设示例中,对象是通过反序列化创建的。

Is there some CDI service I can invoke that will process all the @Inject annotations on an instance of this class, injecting all the matching services currently available? This is needed for the case where objects are not singletons and are not created by CDI. In the above hypothetical example, the object is created by deserialization.

我认为它不可能使用标准CDI。但是,如果你使用DeltaSpike扩展,可以使用的 BeanProvider .injectFields ...做你想要的。请注意,当然您的Pojo不受CDI管理(作用域),只有字段注入被解析...

I dont think its possible with standard CDI. But if you use the DeltaSpike extension, you can use BeanProvider.injectFields ... does what you want. Note that of course your Pojo is not managed (scoped) by CDI, only the field injections are resolved ...