首先分析。
1: 肯定要利用dom4j读取xml配置文件,将所有的bean的配置信息读取出来
2: 利用反射技术,实例化所有的bean
3: 写注解处理器, 利用注解和内省实现依赖对象的注入。
4: 利用XML中<property>信息,通过内省beanUtils实现基本数据类型的注入
实现:
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 |
package cn.gbx.example; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.beanutils.ConvertUtils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; public
class MyClassPathXmlApplicationContext { private
List<DefBean> defBeans = new
ArrayList<DefBean>(); private
Map<String, Object> singletons = new
HashMap<String, Object>(); public
MyClassPathXmlApplicationContext(String filename) { this .readXML(filename); this .instanceBean(); this .injectAnotation(); this .injectXML(); } //注入基本类型的属性 private
void injectXML() { //枚举bean for
(DefBean defBean : defBeans) { Object bean = singletons. get (defBean.getId()); if
(bean != null ) { //枚举该bean的Property看有没有基本数据类型的注入 for
(DefProperty defProperty : defBean.getDefPropertys()) { if
(defProperty.getValue() != null ){ try
{ PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for
(PropertyDescriptor p : ps) { if
(p.getName(). equals (defProperty.getName())) { Method setter = p.getWriteMethod(); //保证有set方法 if
(setter != null ) { Object value = ConvertUtils.convert(defProperty.getValue(), p.getPropertyType()); setter.setAccessible( true ); setter.invoke(bean, value); } break ; } } } catch
(Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } } //注解处理器 private
void injectAnotation() { //枚举实例化的bean for
(String key : singletons.keySet()) { Object bean = singletons. get (key); if
(bean != null ) { /* * 检查setter方法 */ try
{ PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); //枚举每个bean的属性 for
(PropertyDescriptor p : ps) { Method setter = p.getWriteMethod(); Object value = null ; //存在set方法, 并且有setter上边有Resource注解 if
(setter != null
&& setter.isAnnotationPresent(GbxResource. class )){ GbxResource gbxResource = setter.getAnnotation(GbxResource. class ); //若有name 则按name查找 if
(gbxResource.name() != null
&& ! "" . equals (gbxResource.name())) { value = singletons. get (gbxResource.name()); } else
{ //若没有,先安名字查找, 再按数据类型查找 value = singletons. get (p.getName()); if
(value == null ) { for
(String key2 : this .singletons.keySet()) { if
(p.getPropertyType().isAssignableFrom( this .singletons. get (key2).getClass())) { value = this .singletons. get (key2); break ; } } } } setter.setAccessible( true ); setter.invoke(bean, value); } } } catch
(Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * 检查字段 */ Field[] fileds = bean.getClass().getDeclaredFields(); for
(Field field : fileds) { if
(field.isAnnotationPresent(GbxResource. class )) { GbxResource gbxResource = field.getAnnotation(GbxResource. class ); Object value = null ; if
(gbxResource.name() != null
&& ! "" . equals (gbxResource.name())) { value = this .singletons. get (gbxResource.name()); } else
{ value = this .singletons. get (field.getName()); if
(value == null ) { for
(String key2 : this .singletons.keySet()) { if
(field.getType().isAssignableFrom( this .singletons. get (key2).getClass())) { value = this .singletons. get (key2); break ; } } } } field.setAccessible( true ); try
{ field. set (bean, value); } catch
(IllegalAccessException e) { e.printStackTrace(); } } } } } } //实例化bean对象 private
void instanceBean() { for
(DefBean bean : defBeans) { System. out .println(bean.getId() + " : "
+ bean.getClassName()); if
(bean.getClassName() != null
&& ! "" . equals (bean.getClassName())) { try
{ singletons.put(bean.getId(), Class.forName(bean.getClassName()).newInstance()); } catch
(Exception e) { e.printStackTrace(); } } } } //读取xml文件 private
void readXML(String filename) { SAXReader reader = new
SAXReader(); Document document = null ; URL xmlPath = this .getClass().getClassLoader().getResource(filename); try
{ //的到document document = reader.read(xmlPath); //设置命名空间 Map<String, String> nsMap = new
HashMap<String, String>(); //创建查询路径 XPath xPath = document.createXPath( "//ns:beans/ns:bean" ); xPath.setNamespaceURIs(nsMap); List<Element> beans = xPath.selectNodes(document); DefBean defBean = null ; for
(Element e : beans) { String id = e.attributeValue( "id" ); String className = e.attributeValue( "class" ); defBean = new
DefBean(id, className); XPath xPath2 = e.createXPath( "ns:property" ); xPath2.setNamespaceURIs(nsMap); List<Element> propertys = xPath2.selectNodes(e); DefProperty defProperty = null ; for
(Element e2 : propertys) { String name = e2.attributeValue( "name" ); String ref
= e2.attributeValue( "ref" ); String value = e2.attributeValue( "value" ); defProperty = new
DefProperty(name, ref , value); defBean.getDefPropertys().add(defProperty); } defBeans.add(defBean); } } catch
(Exception e) { e.printStackTrace(); } } public
Object getBean(String key) { return
singletons. get (key); } } |
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 |
package cn.gbx.serviceimpl; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import cn.gbx.daoimpl.PersonDao; import cn.gbx.example.GbxResource; import cn.gbx.service.PersonService; public
class PersonServiceImpl implements PersonService { @GbxResource private
PersonDao personDao; private
String name; public
void save() { personDao.save(); System. out .println( "Name = "
+ name); System. out .println( "service层的 save方法" ); } public
void setPersonDao(PersonDao personDao) { this .personDao = personDao; } public
String getName() { return
name; } public
void setName(String name) { this .name = name; } public
PersonDao getPersonDao() { return
personDao; } } |
注解:
1
2
3
4
5
6
7
8
9
10
11
12 |
package cn.gbx.example; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.FIELD}) public
@ interface
GbxResource { public
String name() default
"" ; } |
编码实现Spring 利用@Resource注解实现bean的注入,xml实现基本数据类型的注入
原文:http://www.cnblogs.com/E-star/p/3560070.html