Reputation: 1319
I´m currently working through a JavaEE7 tutorial and I´ve came to an exercise I can´t solve. I have to split my logging into tech log and operational log using qualifiers. Here´s the class where I define these qualifiers:
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
*
* @author jalexakis
*/
public class Logs {
@Qualifier
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface TecLog {}
@Qualifier
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface OperationalLog{}
}
Now I have to change this method,
@Produces
public Logger produceLog(InjectionPoint injectionPoint){
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName(), "messages");
}
into two methods, one for @TecLog and one for @OperationalLog. How do I do this? I tried annotating the method with the qualifiers, but I get an "annotation type not applicable to this kind of declaration"-error.
Upvotes: 0
Views: 930
Reputation: 4970
First remark, even if it might work (I never tested), I wouldn't recommend defining qualifier as inner static classes. In your case there are even non static class so I don't see how you could use them. To make your life simpler make your both qualifier top level class in your application.
Second point, qualifiers can be applied on type, method, parameter and field so the correct target would be:
@Target({ TYPE, METHOD, PARAMETER, FIELD })
That's the origin of your error by the way
So to sum up here is the correct definition for your qualifiers
@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
public @interface TecLog {
}
and
@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
public @interface OperationalLog{
}
as they accept METHOD
as target you can now apply them yo your producers
Upvotes: 3