问题
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