# 项目代码资源:
可能还在审核中,请等待。。。
https://download.csdn.net/download/chenhz2284/89435385
# 项目代码
【pom.xml】
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
【application.properties】
server.port=8080
spring.application.name=myBeanMain
management.server.port=7001
management.endpoints.web.exposure.include=*
【Annotation1.java】
package com.chz.myBeanMain.annotations;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Annotation1 {
String name() default "Annotation1-name";
String p1() default "Annotation1-p1";
}
【Annotation2.java】
package com.chz.myBeanMain.annotations;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Annotation1(name = "at Annotation2: Annotation1-name")
public @interface Annotation2 {
String name() default "Annotation2-name";
}
【Annotation3.java】
package com.chz.myBeanMain.annotations;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Annotation3 {
String name() default "Annotation2-name";
}
【BranchCommonImportSelector.java】
package com.chz.myBeanMain.imports.importSelector;
@Slf4j
public class BranchCommonImportSelector implements ImportSelector
{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata)
{
log.info("chz >>> BranchCommonImportSelector.selectImports()");
MyBeanMainTest2.test(annotationMetadata);
return new String[]{};
}
}
【MyBeanMainTest.java】
package com.chz.myBeanMain;
@Annotation1(name = "at MyBeanMainTest: Annotation1-name")
@Annotation2(name = "at MyBeanMainTest: Annotation2-name")
@Import(BranchCommonImportSelector.class)
@SpringBootApplication
public class MyBeanMainTest {
@Annotation1(name = "at main() method")
public static void main(String[] args) {
SpringApplication.run(MyBeanMainTest.class, args);
}
@Annotation1(name = "at test1() method")
public static void test1()
{
}
@Annotation2(name = "at test2() method")
public static void test2()
{
}
@Annotation3(name = "at test3() method")
public static void test3()
{
}
}
【MyBeanMainTest2.java】
package com.chz.myBeanMain;
public class MyBeanMainTest2 {
public static void main(String[] args) throws Exception
{
// 生成【MyBeanMainTest.class】的【AnnotationMetadata】
AnnotationMetadata annotationMetadata = AnnotationMetadata.introspect(MyBeanMainTest.class);
MyBeanMainTest2.test(annotationMetadata);
}
public static void test(AnnotationMetadata annotationMetadata)
{
StandardAnnotationMetadata standardAnnotationMetadata = (StandardAnnotationMetadata)annotationMetadata;
// 获取注解【Annotation1】的所有属性值
System.out.println("################################################################");
Map<String, Object> annotationAttributes = standardAnnotationMetadata.getAnnotationAttributes(Annotation1.class.getName(), false);
System.out.println(annotationAttributes);
// 合并所有的注解
MergedAnnotations mergedAnnotations = standardAnnotationMetadata.getAnnotations();
// 获取注解【Annotation1】上的【name】属性值
System.out.println("################################################################");
MergedAnnotation<Annotation1> annotation1MergedAnnotation = mergedAnnotations.get(Annotation1.class);
String name = annotation1MergedAnnotation.getString("name");
System.out.println(name);
// 获取主程序类的所有注解名(去重)
System.out.println("################################################################");
Set<String> annotationTypes = standardAnnotationMetadata.getAnnotationTypes();
System.out.println(annotationTypes);
// 列出【MyBeanMainTest】类的所有注解,包括注解的注解
System.out.println("################################################################");
for( MergedAnnotation<Annotation> mergedAnnotation : mergedAnnotations ){
System.out.println(mergedAnnotation.getType());
}
// 获取被【Annotation1】注解的所有方法
System.out.println("################################################################");
Set<MethodMetadata> annotatedMethods = standardAnnotationMetadata.getAnnotatedMethods(Annotation1.class.getName());
for( MethodMetadata methodMetadata : annotatedMethods ){
System.out.println("-----------------------");
System.out.println(methodMetadata.getMethodName());
System.out.println(methodMetadata.getAnnotationAttributes(Annotation1.class.getName()));
}
}
}
运行与测试
运行【MyBeanMainTest】,查看日志
代码与对应输出1:
代码与对应输出2:
代码与对应输出3:
代码与对应输出4:
代码与对应输出5:
这里注意【test2()】方法是通过【@Annotation2】间接引入的【@Annotation1】注解