CDI注入现有对象 [英] CDI inject into existing object

查看:176
本文介绍了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 ...

这篇关于CDI注入现有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆