1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.beans.factory.annotation;
18
19 import java.lang.annotation.Annotation;
20 import java.util.Set;
21
22 import org.springframework.beans.BeansException;
23 import org.springframework.beans.factory.BeanClassLoaderAware;
24 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
25 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
27 import org.springframework.core.Ordered;
28 import org.springframework.util.ClassUtils;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class CustomAutowireConfigurer implements BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered {
50
51 private int order = Ordered.LOWEST_PRECEDENCE;
52
53 private Set<?> customQualifierTypes;
54
55 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
56
57
58 public void setOrder(int order) {
59 this.order = order;
60 }
61
62 @Override
63 public int getOrder() {
64 return this.order;
65 }
66
67 @Override
68 public void setBeanClassLoader(ClassLoader beanClassLoader) {
69 this.beanClassLoader = beanClassLoader;
70 }
71
72
73
74
75
76
77
78
79
80
81
82 public void setCustomQualifierTypes(Set<?> customQualifierTypes) {
83 this.customQualifierTypes = customQualifierTypes;
84 }
85
86
87 @Override
88 @SuppressWarnings("unchecked")
89 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
90 if (this.customQualifierTypes != null) {
91 if (!(beanFactory instanceof DefaultListableBeanFactory)) {
92 throw new IllegalStateException(
93 "CustomAutowireConfigurer needs to operate on a DefaultListableBeanFactory");
94 }
95 DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
96 if (!(dlbf.getAutowireCandidateResolver() instanceof QualifierAnnotationAutowireCandidateResolver)) {
97 dlbf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
98 }
99 QualifierAnnotationAutowireCandidateResolver resolver =
100 (QualifierAnnotationAutowireCandidateResolver) dlbf.getAutowireCandidateResolver();
101 for (Object value : this.customQualifierTypes) {
102 Class<? extends Annotation> customType = null;
103 if (value instanceof Class) {
104 customType = (Class<? extends Annotation>) value;
105 }
106 else if (value instanceof String) {
107 String className = (String) value;
108 customType = (Class<? extends Annotation>) ClassUtils.resolveClassName(className, this.beanClassLoader);
109 }
110 else {
111 throw new IllegalArgumentException(
112 "Invalid value [" + value + "] for custom qualifier type: needs to be Class or String.");
113 }
114 if (!Annotation.class.isAssignableFrom(customType)) {
115 throw new IllegalArgumentException(
116 "Qualifier type [" + customType.getName() + "] needs to be annotation type");
117 }
118 resolver.addQualifierType(customType);
119 }
120 }
121 }
122
123 }