mercredi 12 septembre 2018

Java Custom Annotation with Spring Boot

I am building a SDK that can have access to the Client's code and find the values associated for the annotations. I have two Custom Annotations:

@Constraint(validatedBy = CustomizationValidator.class)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Customization {

String id();
String state();

}

AND

@Constraint(validatedBy = ActionValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String id();
String[] customizationId();
}

Client code looks like this:

@Service
public class ClientService {

   @Customization(id = "test", state = "SomeState")
   public String method2(final String id) {
       return "Doing Something in method 2 " + id;
 }
}

and

@Slf4j
@Component
@Action(id = "TestAction", customizationId = "test")
public class ActionConfigImpl implements ActionConfiguration<String> {


    @Override
    public String getName() {
        return "ActionConfigImpl";
    }

    @Override
    public String execute(final Map map) {
        log.info("Map in Execute: {}", map);
        log.info("In Execute of ActionConfigImpl");
        return "Some";
    }


    @Override
    public void destroy() throws Exception {
        log.info("In destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("In afterPropertiesSet");
    }
}

Annotation Processor has something like the below code to find values of the annotations:

final Class<?> aClass = actionConfiguration.getClass();
final Action action = aClass.getAnnotation(Action.class);

The above code is able to find the value of action if the actionConfiguration is an object(Created by Spring) of ActionConfigImpl for the above class definition.

But If I have a nested annotation with @Action and @Customization in the same class as below then does not work.

@Slf4j
@Component
@Action(id = "TestAction1", customizationId = "testCP1")
public class ActionConfigImpl implements ActionConfiguration<String> {


    @Override
    public String getName() {
        return "ActionConfigImpl";
    }

    @Override
    @Customization(id = "testCP2", state = "sampleState1")
    public String execute(final Map map) {
        log.info("Map in Execute: {}", map);
        log.info("In Execute of ActionConfigImpl");
        return "Some";
    }


    @Override
    public void destroy() throws Exception {
        log.info("In destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("In afterPropertiesSet");
    }
}

For the above code

final Class<?> aClass = actionConfiguration.getClass();
final Action action = aClass.getAnnotation(Action.class);

the value of action is null.

Can't figure out why. Could someone please help?





Aucun commentaire:

Enregistrer un commentaire