I want to write my own annotations, since there were several ideas, but I can not figure out how to do it. It seems easy, but the question is: how to attach business logic to the annotation? What can I do with the data that came to me in the annotations. example: @Annotation(SomeValue.VALUE) . And how can I take this value to do some business logic with him? For example, shove in some method from another library, scroll through it and return the values ..
- fourIf you do not understand this, it means that you do not need it. - user194374
- @kff can then be explained? - raviga
- oneBusiness logic is not attached to the annotation, but to the annotated class. Business data is stored in a class instance, and the annotation parameters simply customize its behavior. In addition, annotation business logic is typically implemented as part of JavaEE containers. - user194374
- oneMore specifically, business logic is associated with annotation, but applies to annotated objects. In general, your question is too general. - user194374
- @kff well, in general terms, I understand it. but this is exactly what I knew. I want to know then. how then to write an annotation .. say datasors, which I could cling to any class and connect datasors through it? - raviga
1 answer
To use your annotation you need:
- Implement annotation (
public @interface MyAnnotationExamplewith annotation type ElementType - for example: class / method / constructor / field, etc.). - Use this annotation
MyAnnotationExamplein your code (according to the type ElementType annotationMyAnnotationExample), i.e. add annotation to class / method / field / etc. - Implement the handler for this annotation through reflection .
Example # 1. Annotation on the class and reading class annotations through reflection.
Create the annotation MyAnnotationExampleForClass :
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value = ElementType.TYPE) //указание что данная аннотация вешается на класс @Retention(value = RetentionPolicy.RUNTIME) //аннотация доступна в процессе работы модуля public @interface MyAnnotationExampleForClass { String name() default "default name"; //в name() мы будем хранить значения аннотации } Create a class MyClassExample using the annotation MyAnnotationExampleForClass :
@MyAnnotationExampleForClass(name = "this is my annotation value!!!") public class MyClassExample { ... } Create a class MyAnnotationProcessorExample to get the annotation values:
class MyAnnotationProcessorExample { MyClassExample myClass = new MyClassExample(); //получаем нашу аннотацию из нашего объекта MyAnnotationExampleForClass a = myClass.getClass().getAnnotation(MyAnnotationExampleForClass.class); System.out.println("выводим значение аннотации = " + a.name()); System.out.println("печатаем тип аннотации со значением = " + a.toString()); } Example # 2. Annotation on the class method and reading the method annotation through reflection. Add another annotation MyAnnotationExampleForMethod for the method:
@Target(value = ElementType.METHOD) @Retention(value = RetentionPolicy.RUNTIME) public @interface MyAnnotationExampleForMethod { String name() default "default name 2"; } Add a new runForest() method to our class MyClassExample (from example # 1 runForest() :
@MyAnnotationExampleForClass(name = "this is my annotation value!!!") public class MyClassExample { @MyAnnotationExampleForMethod(name = "this is my method-annotation value") public void runForest() { System.out.println("Our Forest run very good!"); } } Create a class MyAnnotationProcessorExample2 to get the annotation values:
class MyAnnotationProcessorExample2 { MyClassExample myClass = new MyClassExample(); //получаем наш метод из нашего объекта Method m = tm.getClass().getMethod("runForest"); //получаем аннотацию из нашего метода MyAnnotationExampleForMethod a = m.getAnnotation(TimeAnnotationMethod.class); System.out.println("выводим значение аннотации = " + a.name()); System.out.println("печатаем тип аннотации со значением = " + a.toString()); } Now you can use these handler classes MyAnnotationProcessorExample and MyAnnotationProcessorExample2 in the right places to process business logic. Examples cited the most primitive, only for a common understanding. For more complete implementations, you need to understand the concept of java reflection.
I also recommend to see the topics:
- Ltd. this is what you need. thanks - raviga