注解配置与解析

Time: 2024-05-15 Wednesday 12:21:01
Author: Jackasher

注解配置与解析

注解的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
package org.example.annoation;

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 Component {
String value();
}

注解的取值:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.example;

import org.example.annoation.Component;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) throws ClassNotFoundException {
Class<?> aClass = Class.forName("org.example.pojo.User");
if (aClass.isAnnotationPresent(Component.class)) {
Component annotation = aClass.getAnnotation(Component.class);
String value = annotation.value();
System.out.println(value);
}
}
}

注解配置与解析
http://example.com/2024/05/15/注解配置与解析/
作者
Jack Asher
发布于
2024年5月15日
许可协议