简体   繁体   中英

Custom Annotations in Java

This is the first time I am trying to write a custom annotations in java.

I am not sure whether it is possible or not but wanted to give it a try before approaching another solution.

So here is the scenario, I have a lots of method that sends the data out from the application to a device. I have a requirement to log all these data in database.

I would like to create an annotation for this so that I can write the code in the annotation to log the data in database and then annotation all the methods with this annotation.

I can modify the code to log into the database but in that case I have to go in each method and place my code at correct place inorder to log them into database.

This is the reason I am looking for annotation based approach.

Is it possible what I am looking for or am I asking more.

Any pointers will be appreciated or If someone has different approach for my solution that will be really help full.

Instead of writing your own Annotations and processing them, have a look at what Spring provides, eg Interceptors:

Interceptors vs Aspects in Spring?

You can try below approach

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Todo {
    public enum Priority {LOW, MEDIUM, HIGH}
    String logInfo() default "Logging...";
    Priority priority() default Priority.LOW;
}


package annotation;

public class BusinessLogic {
    public BusinessLogic() {
        super();
    }

    public void compltedMethod() {
        System.out.println("This method is complete");
    }    

    @Todo(priority = Todo.Priority.HIGH)
    public void notYetStartedMethod() {
        // No Code Written yet
    }

    @Todo(priority = Todo.Priority.MEDIUM, logInfo = "Inside DAO")
    public void incompleteMethod1() {
        //Some business logic is written
        //But its not complete yet
    }

    @Todo(priority = Todo.Priority.LOW)
    public void incompleteMethod2() {
        //Some business logic is written
        //But its not complete yet
    }
}


package annotation;
import java.lang.reflect.Method;

public class TodoReport {
    public TodoReport() {
        super();
    }

    public static void main(String[] args) {
        Class businessLogicClass = BusinessLogic.class;
        for(Method method : businessLogicClass.getMethods()) {
            Todo todoAnnotation = (Todo)method.getAnnotation(Todo.class);
            if(todoAnnotation != null) {
                System.out.println(" Method Name : " + method.getName());
                System.out.println(" Author : " + todoAnnotation.logInfo());
                System.out.println(" Priority : " + todoAnnotation.priority());
                System.out.println(" --------------------------- ");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM