通过注解获取对应的类型

  |  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @author klover
* 处理器
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
@Inherited
public @interface Processor {
@AliasFor(
annotation = Component.class
)
String value() default "";

/**
* 执行器
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Process {
String value() default "";
}
}
  1. 通过 SpringContextUtil 获取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.qbit.common.utils;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.Map;

@Component
public class SpringContextUtil implements ApplicationContextAware {

private static ApplicationContext context = null;

@Override
public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}

/**
* 获取当前环境
*
* @return env
*/
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}

public static boolean isProd() {
return "prod".equals(getActiveProfile());
}

/**
* 获取applicationContext
*
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return context;
}

/**
* 通过Annotation获取 Bean.
*
* @param clazz 注解类
* @return Map<String, Object>
*/
public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> clazz) {
return context.getBeansWithAnnotation(clazz);
}

/**
* 获取注解的类名
* @param clazz 注解类
* @return String[]
*/
public static String[] getBeanNamesForAnnotation(Class<? extends Annotation> clazz){
return context.getBeanNamesForAnnotation(clazz);
}

/**
* 通过name获取 Bean.
*
* @param name 名称
* @return Object
*/
public static Object getBean(String name) {
return context.getBean(name);
}

/**
* 通过class获取Bean.
*
* @param clazz 类
* @param <T> 类
* @return <T>
*/
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}

}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Map<String, Object> beans = SpringContextUtil.getBeansWithAnnotation(Processor.class);
for (String key : beans.keySet()) {
Object oldBean = beans.get(key);
Object bean = oldBean;
// 开启aop后getAnnotation拿不到原生类 所以需要这个
while (AopUtils.isAopProxy(bean)) {
try {
bean = ((Advised) bean).getTargetSource().getTarget();
} catch (Exception e) {
throw new RuntimeException("get target bean failed", e);
}
}

assert bean != null;

Class<?> clazz = bean.getClass();
System.out.println(clazz.getName());

Processor annotation = clazz.getAnnotation(Processor.class);
System.out.println(annotation);
Method[] methods = clazz.getDeclaredMethods();

for (Method method : methods) {
if (method.isAnnotationPresent(Processor.Process.class)) {
System.out.println(method);
Method method1 = oldBean.getClass().getMethod(method.getName());
method1.invoke(oldBean);
}
}
}
  1. 通过 Reflections 获取
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
Reflections f = new Reflections(packageName);
Set<Class<?>> set = f.getTypesAnnotatedWith(Processor.class);
for (Class<?> c : set) {
Object bean = c.getDeclaredConstructor().newInstance();
Processor annotation = c.getAnnotation(Processor.class);
Method[] methods = bean.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Processor.Process.class)) {
System.out.println(method);
}
}
beanContainer.put(annotation.value(), bean);
}
文章目录