I'm studying the annotations and I'm trying to create a custom annotation like Lombook annotations (@Getter and @Setter), that is, an annotation that works as a method.
I have my annotation:
package com.example.demo.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Dragonrider {
String name();
}
I have a class that use this annotation:
package com.example.demo.model;
import com.example.demo.annotations.Dragonrider;
@Dragonrider(name = "Daenerys")
public class Dragon {
String name;
boolean hasFire;
double weight;
public Dragon(String name, boolean hasFire, double weight){
this.name = name;
this.hasFire = hasFire;
this.weight = weight;
}
public void dracarys() {
System.out.println("DRACARYS!");
}
}
I don’t know how to go from here. I have tried to create an Aspect to intercept the annotation like this:
package com.example.demo.annotations;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class DragonriderAspect {
@After("@annotation(com.example.demo.annotations.Dragonrider)")
public void fly(){
System.out.println("Is flying!");
}
}
But it does not works.
I don't know how the custom annotation works, I guess creating an Aspect isn't what I need.