Steve
Steve

Reputation: 8809

CDI inject into existing object

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.

Upvotes: 2

Views: 598

Answers (1)

Jan Galinski
Jan Galinski

Reputation: 11993

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

Upvotes: 2

Related Questions