CDI inject into existing object

旧街凉风 提交于 2019-12-24 10:26:36

问题


Let's say I have the following class:

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;
}

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.


回答1:


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 ...



来源:https://stackoverflow.com/questions/24750967/cdi-inject-into-existing-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!