import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value(); int count() default 1; } import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String value(); int count() default 1; } public class MyClass { @MyAnnotation(value = "Hello", count = 5) public void myMethod() { } } import java.lang.reflect.*; public class MyAnnotationProcessor { public static void processAnnotations(Class<?> clazz) { Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); String value = annotation.value(); int count = annotation.count(); System.out.println("Value: " + value); System.out.println("Count: " + count); } } } } <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <annotationProcessorPaths> <path> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>${spring.boot.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>


上一篇:
下一篇:
切换中文