The use scene of the "annotation" framework in the Java class library
The use scene of the "annotation" framework in the Java class library
introduction:
Note is a mechanism that provides metadata in Java language.It adds additional information to the various elements (including classes, methods, variables, etc.) in the program so that it can be used during compilation, runtime or during code generation.The annotation framework is an important feature of the Java class library, which provides a simple and flexible way to expand and customize the code.This article will introduce the use scenarios of the annotation framework and provide some specific examples.
scenes to be used:
1. Generate code during compilation: The annotation can generate some additional code according to the information in the annotation, so as to achieve automated code generation.A typical example is the code using an annotation to generate the code of the database access object (DAO).By adding annotations to the DAO interface method, the specific class to achieve the interface can be automatically generated during compilation.
public interface UserDao {
@Insert("INSERT INTO user (name, age) VALUES (:name, :age)")
void addUser(@Param("name") String name, @Param("age") int age);
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Insert {
String value();
}
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.SOURCE)
public @interface Param {
String value();
}
2. Dynamic proxy during runtime: Dynamic proxy during runtime can be achieved by using annotations.Dynamic proxy is a technology that generates proxy and proxy during runtime, so that we can add some additional logic before and after the method of implementation without modifying the original code.By adding annotations to the interface method, additional logic can be added according to the information in the annotation class.
public interface UserService {
@Log
void addUser(String name);
String getUser(String name);
}
public class UserServiceImpl implements UserService {
@Override
public void addUser(String name) {
System.out.println("Adding user: " + name);
}
@Override
public String getUser(String name) {
System.out.println("Getting user: " + name);
return name;
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {}
public class LogProxy implements InvocationHandler {
private Object target;
public LogProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.isAnnotationPresent(Log.class)) {
System.out.println("Log: Method " + method.getName() + " is called.");
}
return method.invoke(target, args);
}
}
public class Main {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
UserService proxy = (UserService) Proxy.newProxyInstance(
userService.getClass().getClassLoader(),
userService.getClass().getInterfaces(),
new LogProxy(userService)
);
proxy.addUser("Alice"); // Output: Log: Method addUser is called. Adding user: Alice
proxy.getUser("Bob"); // Output: Log: Method getUser is called. Getting user: Bob
}
}
3. Building tools: The annotation framework is very suitable for building tools.You can use annotations to add configuration information to custom tasks to achieve a stronger and flexible construction process.For example, using the annotation framework can add custom tasks to build scripts for Maven or Gradle.
@Mojo(name = "custom-task")
public class CustomTaskMojo extends AbstractMojo {
@Parameter(property = "message", required = true)
private String message;
public void execute() throws MojoExecutionException {
getLog().info("Message: " + message);
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Mojo {
String name();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Parameter {
String property();
boolean required();
}
in conclusion:
The annotation framework in the Java class library provides a powerful mechanism that can be used to add meta data during compilation, runtime and building tools.By using annotations, developers can realize automated code generation, dynamic proxy during runtime, and the expansion function of building tools.The annotation framework provides more flexible and simple ways to expand and customize code for Java development.