/
CustomizeInjectTest.java
131 lines (108 loc) · 3.55 KB
/
CustomizeInjectTest.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.wcong.test.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
@Configuration
public class CustomizeInjectTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(CustomizeInjectTest.class);
annotationConfigApplicationContext.refresh();
BeanClass beanClass = annotationConfigApplicationContext.getBean(BeanClass.class);
beanClass.print();
FullInjectBeanClass fullInjectBeanClass = annotationConfigApplicationContext.getBean(FullInjectBeanClass.class);
fullInjectBeanClass.print();
fullInjectBeanClass.superPrint();
}
@Component
public static class BeanClass {
@MyInject
private FieldClass fieldClass;
public void print() {
fieldClass.print();
}
}
@Component
public static class FieldClass {
public void print() {
System.out.println("hello world");
}
}
public static class FullInjectSuperBeanClass {
private FieldClass superFieldClass;
public void superPrint() {
superFieldClass.print();
}
}
@Component
@FullInject
public static class FullInjectBeanClass extends FullInjectSuperBeanClass {
private FieldClass fieldClass;
public void print() {
fieldClass.print();
}
}
@Component
public static class MyInjectBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
private ApplicationContext applicationContext;
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (hasAnnotation(bean.getClass().getAnnotations(), FullInject.class.getName())) {
Class beanClass = bean.getClass();
do {
Field[] fields = beanClass.getDeclaredFields();
for (Field field : fields) {
setField(bean, field);
}
} while ((beanClass = beanClass.getSuperclass()) != null);
} else {
processMyInject(bean);
}
return bean;
}
private void processMyInject(Object bean) {
Class beanClass = bean.getClass();
do {
Field[] fields = beanClass.getDeclaredFields();
for (Field field : fields) {
if (!hasAnnotation(field.getAnnotations(), MyInject.class.getName())) {
continue;
}
setField(bean, field);
}
} while ((beanClass = beanClass.getSuperclass()) != null);
}
private void setField(Object bean, Field field) {
if (!field.isAccessible()) {
field.setAccessible(true);
}
try {
field.set(bean, applicationContext.getBean(field.getType()));
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean hasAnnotation(Annotation[] annotations, String annotationName) {
if (annotations == null) {
return false;
}
for (Annotation annotation : annotations) {
if (annotation.annotationType().getName().equals(annotationName)) {
return true;
}
}
return false;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
}