杰瑞科技汇

Java annotation怎么用?注解核心概念解析

Of course! Let's dive deep into Java annotations. We'll cover what they are, why they're used, how to create them, and how to use them.

Java annotation怎么用?注解核心概念解析-图1
(图片来源网络,侵删)

What is an Annotation in Java?

In simple terms, an annotation is a form of metadata (data about data) that you can add to your Java source code. It doesn't change the logic of your program but provides additional information about the program, which can then be used by the compiler, tools, or the runtime environment.

Think of it like a sticky note you attach to a piece of code to give instructions or mark something important for a tool (like a compiler or a testing framework) to see.

Key Characteristics:

  • Metadata: It provides data about the program, not the program logic itself.
  • Doesn't Affect Code Logic: An annotation by itself does nothing. It needs a "processor" to read and act upon it.
  • Declarative: You declare them using the symbol.

Why Use Annotations? (The Purpose)

Annotations make code more readable and reduce boilerplate code. They are used for a wide variety of purposes:

Java annotation怎么用?注解核心概念解析-图2
(图片来源网络,侵删)
  1. Compile-Time Checks: The compiler can use annotations to validate your code.

    • Example: @Override tells the compiler to check if the method you're annotating actually overrides a method from a superclass. If not, it will give a compile-time error.
  2. Code Generation: Tools can read annotations and generate code, interfaces, or other files automatically.

    • Example: @Entity, @Table, @Column from JPA (Java Persistence API) tell a persistence framework (like Hibernate) how to create a database table from your Java class.
  3. Reflection at Runtime: Some annotations are available at runtime, allowing the application to inspect its own structure and behavior.

    • Example: @WebServlet in a Java web application tells the server (like Tomcat) how to map a servlet to a specific URL.
  4. Configuration and Dependency Injection: Frameworks like Spring use annotations to configure the application and manage dependencies.

    Java annotation怎么用?注解核心概念解析-图3
    (图片来源网络,侵删)
    • Example: @Autowired tells the Spring framework to automatically "inject" a required dependency into a class.

Built-in Java Annotations

Java comes with several useful annotations out of the box.

Annotation Purpose Example
@Override Informs the compiler that the annotated method is intended to override a method in a superclass. @Override <br> public String toString() { ... }
@Deprecated Marks a method, class, or field as outdated. Using it will generate a warning from the compiler. @Deprecated <br> public void oldMethod() { ... }
@SuppressWarnings Instructs the compiler to suppress specific warnings. Commonly used to suppress unchecked or rawtype warnings. @SuppressWarnings("unchecked") <br> List list = new ArrayList();
@SafeVarargs Applied to a variable-arity method (varargs) to assert that the code does not perform unsafe operations on its varargs parameter. @SafeVarargs <br> public static <T> void doSomething(T... args) { ... }
@FunctionalInterface Ensures that the annotated interface is a functional interface (i.e., it has exactly one abstract method). @FunctionalInterface <br> public interface MyRunnable { <br> &nbsp;&nbsp;void run(); <br> }

Creating Custom Annotations (Meta-Annotations)

You can create your own annotations to suit your specific needs. This is done using the @interface keyword.

Step-by-Step Example: Creating a @Version Annotation

Let's create a simple annotation to mark the version of a class.

a. Define the Annotation

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 1. Target: Where can this annotation be applied?
//    Here, it can only be applied to types (classes, interfaces, enums).
@Target(ElementType.TYPE)
// 2. Retention: When is this annotation available?
//    Here, it's available at runtime so we can read it via reflection.
@Retention(RetentionPolicy.RUNTIME)
// 3. The annotation itself
public @interface Version {
    // This is an element, not a method.
    // It has a default value of "1.0".
    String value() default "1.0";
}

Explanation of Meta-Annotations:

  • @Target: Specifies where the annotation can be used. ElementType.TYPE means it can only be applied to classes, interfaces, and enums. Other options include ElementType.METHOD, ElementType.FIELD, etc.
  • @Retention: Specifies how long the annotation should be retained.
    • RetentionPolicy.SOURCE: Discarded by the compiler (e.g., @SuppressWarnings).
    • RetentionPolicy.CLASS: Discarded by the JVM (default behavior).
    • RetentionPolicy.RUNTIME: Available for reflection at runtime. This is the most powerful option.

b. Use the Custom Annotation

Now, let's apply our @Version annotation to a class.

@Version(value = "2.1") // Using the annotation
public class MyService {
    public void performTask() {
        System.out.println("Task performed by MyService v2.1");
    }
}

c. Process the Annotation (Using Reflection)

To make the annotation do something, we need to write code that reads it. We'll use Java Reflection.

import java.lang.annotation.Annotation;
public class AnnotationProcessor {
    public static void main(String[] args) {
        // Get the Class object for MyService
        Class<MyService> serviceClass = MyService.class;
        // Check if the @Version annotation is present
        if (serviceClass.isAnnotationPresent(Version.class)) {
            // Get the annotation instance
            Version versionAnnotation = serviceClass.getAnnotation(Version.class);
            // Read the value from the annotation
            String version = versionAnnotation.value();
            System.out.println("Found version annotation for " + serviceClass.getSimpleName());
            System.out.println("Version: " + version);
        } else {
            System.out.println("No @Version annotation found for " + serviceClass.getSimpleName());
        }
    }
}

Output:

Found version annotation for MyService
Version: 2.1

Annotations in Popular Frameworks (Examples)

This is where annotations truly shine, as they form the backbone of modern Java development.

a. Spring Framework

  • @Component: Marks a class as a Spring bean (a Spring-managed object).
  • @Service: Specializes @Component for service-layer classes.
  • @Repository: Specializes @Component for data access layer classes (like DAOs).
  • @Autowired: Tells Spring to automatically wire a dependency.
  • @Value: Injects values from a properties file (e.g., @Value("${app.name}")).

Example:

@Service // Marks this class as a Spring service
public class UserService {
    @Autowired // Automatically injects an instance of UserRepository
    private UserRepository userRepository;
    public void addUser(User user) {
        userRepository.save(user);
    }
}

b. JPA (Java Persistence API) / Hibernate

  • @Entity: Marks a class as a JPA entity, which can be persisted to a database.
  • @Table: Specifies the table name in the database.
  • @Id: Specifies the primary key of the entity.
  • @GeneratedValue: Specifies the strategy for generating the primary key value.
  • @Column: Specifies the column name and other properties.

Example:

@Entity // Marks this class as a database table
@Table(name = "users") // Maps to the 'users' table
public class User {
    @Id // Marks this field as the primary key
    @GeneratedValue(strategy = GenerationType.IDENTITY) // Auto-increment the ID
    private Long id;
    @Column(name = "username", nullable = false, length = 50) // Maps to a column
    private String username;
    // Getters and Setters...
}

Summary

Feature Description
What it is Metadata (data about data) attached to code.
How it looks Starts with , e.g., @Override, @Service.
Purpose To provide information to compilers, tools, and the runtime.
Key Benefits Reduces boilerplate, improves readability, enables powerful frameworks.
How to create Use @interface keyword, and define @Target and @Retention.
How to use Apply with @MyAnnotation and process using Reflection.

Annotations are a fundamental and powerful feature of modern Java, enabling developers to write cleaner, more declarative, and more maintainable code.

分享:
扫描分享到社交APP
上一篇
下一篇