Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI: Obtaining InjectionPoint when producing a scoped bean

Tags:

java

cdi

whats the easiest way to obtain the InjectionPoint when your producer method is producing a scoped bean? For some reason the spec does only allow injection of the InjectionPoint when you are producing scoped beans. Is there an easy way to do that or some kind of bolg entry or tutorial for that?

For clarifiaction, here is what Id like to do, but what is forbidden by the spec:

@Produces
@RequestScoped
@MyAnnotation    
private MyObject produce(InjectioPoint ip){
....
}
like image 985
PhilBa Avatar asked Feb 25 '15 15:02

PhilBa


1 Answers

Let's imaging that what you'd like to do is possible. You have an @ApplicationScoped bean defined:

@Produces
@ApplicationScoped
String produceMyString(InjectionPoint ip) {}

And you have two injection points for that bean:

@Inject
String myString1;

@Inject
String myString2;

As the bean is ApplicationScoped, the producer method will get called only once. Which leads to the conclusion that it is impossible as the container cannot choose what injection point to use as a parameter of the producer method.

This explains why accessing the InjectionPoint metadata forces the bean to be @Dependent. Stated another way, the CDI specification forbids that behavior because it leads to a contradiction and not because of an arbitrary choice.

like image 180
Antonin Stefanutti Avatar answered Oct 13 '22 00:10

Antonin Stefanutti