在Spring中有两个非常重要的概念,控制反转和依赖注入;控制反转将依赖对象的创建和管理交由Spring容器,而依赖注入则是在控制反转的基础上将Spring容器管理的依赖对象注入到应用之中;

所谓依赖注入:在运行期,由外部容器动态将依赖对象注入到组件中。

XML文件解析 + Java反射技术;

首先是XML文件的解析(dom4j),Spring框架对于配置文件的选择是XML文件,根据Spring的规范,配置文件的命名是没有特殊要求的,只是在文件的放置位置上有两种选择;类路径下或者操作系统文件目录下(大多数情况是放到类路径下)。

对于Spring的控制反转和依赖注入来说,唯一使用的是配置文件中的<bean>标签,通过这个标签,Spring就完成了对象的创建和依赖对象的注入工作;

1、首先对于配置文件中的<bean>节点,在Spring框架中存在一个对用的定义接口,叫做BeanDefinition;子啊个类定义了获得<bean>节点中出现的所有属性的方法,例如classNam、scope、factory-method、lazy-init 等等属性;

2、对于<bean>节点的子节点property则完成了属性注入的功能;属性注入有三种方式,构造器注入、属性setter方法注入和注解方式注入;

3、如果是setter方法注入,对于类属性XML配置文件中有两种方法,一是使用property节点的ref属性,一是使用property几点的子节点bean进行内部bean配置;如果是对于基本数据类型进行配置,那么要是用property节点的value属性;

定义自己的关于bean节点、property节点的pojo类文件;

使用注入DOM4J等开源包讲配置文件解析读入;

使用Java的反射技术讲配置文件中的信息setter到我们需要的属性中去;common-beanutils.jar

  1. <context:component-scan base-package="com.sample"/>
  2. <bean id="personService" class="com.spring.junit.test.impl.PersonServiceImpl"></bean>
  3. <bean id="stockService" class="com.spring.junit.test.impl.StockServiceImpl"></bean>
  4. <bean id="personServiceFactory" class="com.spring.junit.test.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBeanFactory"></bean>
  5. <bean id="personServiceFactory2" class="com.spring.junit.test.impl.PersonServiceBeanFactory"></bean>
  6. <bean id="stockServiceFactory" factory-bean="personServiceFactory2" factory-method="createStockServiceBeanFactory"></bean>
  7. <bean id="randomBean" class="com.spring.junit.bean.StaticFactoryBean" factory-method="createRandom" scope="prototype"></bean>
  8. <!-- 集合类型的注入 -->
  9. 通过setter方法注入
  10. <bean id="user" class="com.sample.bean.User"></bean>
  11. <bean id="personDao" class="com.sample.dao.impl.PersonDaoBeanImpl"></bean>
  12. <bean id="personService" class="com.sample.service.impl.PersonServiceBeanImpl">
  13. <property name="personDao" ref="personDao"></property>
  14. <property name="name" value="jackjson_xu_test"></property>
  15. <property name="id" value="108"></property>
  16. <property name="sets">
  17. <set>
  18. <value>第一个</value>
  19. <value>第二个</value>
  20. <value>第三个</value>
  21. </set>
  22. </property>
  23. <property name="lists">
  24. <list>
  25. <value>第一個list元素</value>
  26. <value>第二個list元素</value>
  27. <value>第三個list元素</value>
  28. </list>
  29. </property>
  30. <property name="properties">
  31. <props>
  32. <prop key="key1">value1</prop>
  33. <prop key="key2">value2</prop>
  34. <prop key="key3">value3</prop>
  35. </props>
  36. </property>
  37. <property name="maps">
  38. <map>
  39. <entry key="key-1" value="value-1"></entry>
  40. <entry key="key-2" value="value-2"></entry>
  41. <entry key="key-3" value="value-3"></entry>
  42. <entry key="key-4" value="value-4"></entry>
  43. </map>
  44. </property>
  45. <property name="users">
  46. <map>
  47. <entry key="U_1001">
  48. <ref bean="user"/>
  49. </entry>
  50. <entry key="U_1002">
  51. <ref bean="user"/>
  52. </entry>
  53. </map>
  54. </property>
  55. </bean>
  56. <!-- 采用内部bean的方式注入 -->
  57. <bean id="personService" class="com.sample.service.impl.PersonServiceBeanImpl">
  58. <property name="personDao">
  59. <bean class="com.sample.dao.impl.PersonDaoBeanImpl"/>
  60. </property>
  61. <property name="name" value="jackjson_xu_test"></property>
  62. <property name="id" value="100"></property>
  63. </bean>
  64. <!-- 构造器注入方式 -->
  65. <bean id="personDao" class="com.sample.dao.impl.PersonDaoBeanImpl"></bean>
  66. <bean id="personService2" class="com.sample.service.impl.PersonServiceBeanImpl2" autowire="byType">
  67. <constructor-arg index="0" type="com.sample.dao.IPersonDao" ref="personDao"></constructor-arg>
  68. <constructor-arg index="1" type="java.lang.String" value="http://www.woyo.com"></constructor-arg>
  69. </bean>

package com.sample.junit;

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. /**
  4. * Spring xml 属性的方法
  5. * @author DY
  6. *
  7. */
  8. public class BeanDefinition {
  9. private String id;
  10. private String className;
  11. private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>();
  12. public BeanDefinition(String id, String className) {
  13. this.id = id;
  14. this.className = className;
  15. }
  16. public String getId() {
  17. return id;
  18. }
  19. public void setId(String id) {
  20. this.id = id;
  21. }
  22. public String getClassName() {
  23. return className;
  24. }
  25. public void setClassName(String className) {
  26. this.className = className;
  27. }
  28. public List<PropertyDefinition> getPropertys() {
  29. return propertys;
  30. }
  31. public void setPropertys(List<PropertyDefinition> propertys) {
  32. this.propertys = propertys;
  33. }
  34. }

package com.sample.junit;

  1. /**
  2. * Spring xml bean 子节点property属性方法
  3. *
  4. * @author DY
  5. *
  6. */
  7. public class PropertyDefinition {
  8. private String name;
  9. private String ref;
  10. private String value;
  11. public PropertyDefinition(String name, String ref, String value) {
  12. this.name = name;
  13. this.ref = ref;
  14. this.value = value;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getRef() {
  23. return ref;
  24. }
  25. public void setRef(String ref) {
  26. this.ref = ref;
  27. }
  28. public String getValue() {
  29. return value;
  30. }
  31. public void setValue(String value) {
  32. this.value = value;
  33. }
  34. }

package com.sample.junit;

  1. import java.beans.Introspector;
  2. import java.beans.PropertyDescriptor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import java.net.URL;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import org.apache.commons.beanutils.ConvertUtils;
  11. import org.apache.log4j.Logger;
  12. import org.dom4j.Document;
  13. import org.dom4j.Element;
  14. import org.dom4j.XPath;
  15. import org.dom4j.io.SAXReader;
  16. /**
  17. * spring装配applicationContext.xml文件
  18. * @author DY
  19. *
  20. */
  21. public class SampleClassPathXMLApplicationContext {
  22. private Logger logger = Logger.getLogger(SampleClassPathXMLApplicationContext.class);
  23. private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
  24. private Map<String, Object> sigletons = new HashMap<String, Object>();
  25. public SampleClassPathXMLApplicationContext(String filename) {
  26. this.readXML(filename);
  27. this.instanceBeans();   //bean的实例化 Class.forName().newInstance()
  28. this.annotationInject();//注解
  29. this.injectObject();    //bean对象的属性注入值
  30. }
  31. /**
  32. * 注解处理器
  33. * 如果注解SampleResouce配置了name属性,则根据name所指定的名称获取要注入的实例引用
  34. * 如果注解SampleResouce没有配置name属性,则根据属性所属类型来扫描配置文件获取要注入的实例引用
  35. */
  36. private void annotationInject() {
  37. for (String beanName : sigletons.keySet()) {
  38. Object bean = sigletons.get(beanName);
  39. if (bean != null) {
  40. this.propertyAnnotation(bean);
  41. this.fieldAnnotation(bean);
  42. }
  43. }
  44. }
  45. /**
  46. * 处理在所有set方法加入的注解
  47. * @param bean 处理的bean对象
  48. */
  49. private void propertyAnnotation(Object bean) {
  50. try {
  51. //获取其属性的描述
  52. PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
  53. for (PropertyDescriptor properdesc : ps) {
  54. //获取属性的setter方法
  55. Method setter = properdesc.getWriteMethod();
  56. //setter方法上是否存在注解
  57. if (setter != null && setter.isAnnotationPresent(SampleResource.class)) {
  58. //获取当前注解,判断name属性是否为空
  59. SampleResource resouce = setter.getAnnotation(SampleResource.class);
  60. Object value = null;
  61. if (resouce.name() != null && !"".equals(resouce.name())) {
  62. value = sigletons.get(resouce.name());
  63. setter.setAccessible(true);
  64. setter.invoke(bean, value);//把引用对象注入到属性
  65. } else {//如果当前属性没有指定name,则根据类型匹配
  66. value = sigletons.get(resouce.name());
  67. if (value == null) {
  68. for (String key : sigletons.keySet()) {
  69. //判断当前属性所属类型是否在配置文件中存在
  70. if (properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())) {
  71. value = sigletons.get(key); //获取类型匹配的实例对象
  72. }
  73. }
  74. }
  75. //允许访问private方法
  76. setter.setAccessible(true);
  77. //把引用对象注入属性
  78. setter.invoke(bean, value);
  79. }
  80. }
  81. }
  82. } catch (Exception e) {
  83. logger.error(e.getLocalizedMessage());
  84. }
  85. }
  86. /**
  87. * 处理在字段上的注解
  88. * @param bean
  89. */
  90. private void fieldAnnotation (Object bean) {
  91. try {
  92. //获取全部属性对象数组
  93. Field[] fields = bean.getClass().getFields();
  94. for (Field field : fields) {
  95. if (field.isAnnotationPresent(SampleResource.class)) {
  96. SampleResource resouce = field.getAnnotation(SampleResource.class);
  97. Object value = null;
  98. if (resouce.name() != null && !"".equals(resouce.name())) {
  99. value = sigletons.get(resouce.name());
  100. } else {
  101. value = sigletons.get(field.getName());
  102. if (value == null) {
  103. for (String key : sigletons.keySet()) {
  104. //根据字段类型匹配
  105. if (field.getType().isAssignableFrom(sigletons.get(key).getClass())) {
  106. value = sigletons.get(key);
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. field.setAccessible(true);
  113. field.set(bean, value);
  114. }
  115. }
  116. } catch (Exception e) {
  117. e.getLocalizedMessage();
  118. logger.error("字段注解解析异常:" + e.getLocalizedMessage());
  119. }
  120. }
  121. /**
  122. * 为bean对象的属性注入值
  123. */
  124. private void injectObject() {
  125. for (BeanDefinition beanDefinition : beanDefines) {
  126. Object bean = sigletons.get(beanDefinition.getId());
  127. if (bean != null) {
  128. try {
  129. PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
  130. for (PropertyDefinition propertyDefinition : beanDefinition.getPropertys()) {
  131. for (PropertyDescriptor properdesc : ps) {
  132. if (propertyDefinition.getName().equals(properdesc.getName())) {
  133. Method setter = properdesc.getWriteMethod();// 获取属性的setter方法
  134. if (setter != null) {
  135. Object value = null;
  136. if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim())) {
  137. value = sigletons.get(propertyDefinition.getRef());
  138. } else {
  139. value = ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType());
  140. }
  141. setter.setAccessible(true);//私有方法给与访问权限
  142. setter.invoke(bean, value);// 把引用对象注入到属性
  143. }
  144. break;
  145. }
  146. }
  147. }
  148. } catch (Exception e) {
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * 完成bean的实例化
  155. */
  156. private void instanceBeans() {
  157. for (BeanDefinition beanDefinition : beanDefines) {
  158. try {
  159. if (beanDefinition.getClassName() != null
  160. && !"".equals(beanDefinition.getClassName().trim()))
  161. sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. }
  167. /**
  168. * 读取xml配置文件
  169. *
  170. * @param filename
  171. */
  172. private void readXML(String filename) {
  173. SAXReader saxReader = new SAXReader();
  174. Document document = null;
  175. try {
  176. URL xmlpath = this.getClass().getClassLoader().getResource(filename);
  177. document = saxReader.read(xmlpath);
  178. Map<String, String> nsMap = new HashMap<String, String>();
  179. nsMap.put("ns", "http://www.springframework.org/schema/beans");// 加入命名空间
  180. XPath xsub = document.createXPath("//ns:beans/ns:bean");// 创建beans/bean查询路径
  181. xsub.setNamespaceURIs(nsMap);// 设置命名空间
  182. List<Element> beans = xsub.selectNodes(document);// 获取文档下所有bean节点
  183. for (Element element : beans) {
  184. String id = element.attributeValue("id");// 获取id属性值
  185. String clazz = element.attributeValue("class"); // 获取class属性值
  186. BeanDefinition beanDefine = new BeanDefinition(id, clazz);
  187. XPath propertysub = element.createXPath("ns:property");
  188. propertysub.setNamespaceURIs(nsMap);// 设置命名空间
  189. List<Element> propertys = propertysub.selectNodes(element);
  190. for (Element property : propertys) {
  191. String propertyName = property.attributeValue("name");
  192. String propertyref = property.attributeValue("ref");
  193. String propertyValue = property.attributeValue("value");
  194. PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref, propertyValue);
  195. beanDefine.getPropertys().add(propertyDefinition);
  196. System.out.println("propertyName:" + propertyName + "|propertyref:" + propertyref + "|propertyValue:" + propertyValue);
  197. }
  198. beanDefines.add(beanDefine);
  199. }
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. }
  204. /**
  205. * 获取bean实例
  206. *
  207. * @param beanName
  208. * @return
  209. */
  210. public Object getBean(String beanName) {
  211. return this.sigletons.get(beanName);
  212. }
  213. }

package com.sample.junit;

  1. import org.junit.BeforeClass;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.sample.service.IPersonService;
  6. public class SpringTest {
  7. static ApplicationContext ctx = null;
  8. @BeforeClass
  9. public static void setUpBeforeClass() throws Exception {
  10. ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
  11. }
  12. @Test public void instanceSpring(){
  13. IPersonService personService = (IPersonService)ctx.getBean("personService");
  14. System.out.println(personService);
  15. personService.save();
  16. }
  17. }

最新文章

  1. Unity 处理IOC AOP
  2. [HTML/HTML5]4 使用文本
  3. C#:优惠券代码
  4. C# 6.0的属性(Property)的语法与初始值
  5. linux网络:常用命令(一)
  6. CSS3 贝塞尔曲线实现
  7. Cesium之球心坐标与本地坐标
  8. Elasticsearch聚合——aggregation
  9. 通过配置tomcat实现项目免部署
  10. postgresql修改数据库编码
  11. 并发系列3:Lock锁以及核心类AQS
  12. [转] web无插件播放RTSP摄像机方案,拒绝插件,拥抱H5!
  13. 牛客网剑指Offer——正则表达式匹配
  14. 有关于分布式和SOA的理解
  15. linux(centos 7) 下安装nodejs
  16. SQLAlchemy 关联表删除实验
  17. Downloading jQuery 3.2.1
  18. mysql安装(rpm)
  19. for循环打印等腰三角形、直角三角形、菱形
  20. POJ1159解题心得

热门文章

  1. git 错误 fatal: loose object...is corrupt
  2. redux-thunk中间件源码
  3. Centos7环境下安装python3.6.4 并与python2共存
  4. BZOJ2124: 等差子序列(树状数组&amp;hash -&gt; bitset 求是否存在长度为3的等差数列)
  5. MyBatis_Study_002(进阶,增删改查)
  6. 【spring源码学习】Spring @PostConstruct和@PreDestroy实例
  7. 使用jsonschema2pojo-maven-plugin 插件根据json文件生成代码
  8. postgraphile 基本试用
  9. C#对Jason序列化匿名对象
  10. 关于 javascript:void(0) 的问题.