for(Annotation a : injectionPoint.getQualifiers())
{
if(a instanseof MyCar)
{
MyCar myCar = (MyCar) a;
a.mpg();
}
}
How to read non-binding attribute using java cdi's injection point
-
01-10-2022 - |
Domanda
If I have an InjectPoint
class instance, how do I read the attributes of the Annotation from it. i.e: annotated with Qualifier @MyCar(mpg="23")
How would I get the mpg and "23"
if I have an injectionPoint
when MyCar
is injected?
@Inject
public void injectionTest(@MyCar(mpg="23") Car _car,InjectionPoint ip)
{
...
}
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface MyCar
{
@Nonbinding
String mpg() default "30";
}
Soluzione
Altri suggerimenti
You can avoid iterating through the annotations by using the getAnnotated method. You can also access other annotations this way, not just qualifiers.
MyCar myCar = ip.getAnnotated().getAnnotation(MyCar.class);
System.out.println(myCar.mpg());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow