문제

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";
}
도움이 되었습니까?

해결책

for(Annotation a : injectionPoint.getQualifiers())
{
   if(a instanseof MyCar)
   {
     MyCar myCar = (MyCar) a;
     a.mpg();
   }
}

다른 팁

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());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top