http://blog.csdn.net/yerenyuan_pku/article/details/52856465

上回我们已经讲到了Spring依赖注入的第一种方式,现在我们来详解第二种方式,须知这一切都是以编码剖析Spring依赖注入的原理案例为基础的。 
我们将Spring的配置文件——beans.xml的内容改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 依赖注入的第一种方式 --> <!-- <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="personDao" ref="personDao"></property> </bean> --> <!-- 依赖注入第二种方式: --> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="personDao"> <bean class="cn.itcast.dao.impl.PersonDaoBean"></bean> </property> </bean> </beans>

这种方式是使用内部bean依赖注入,其缺点就是:该bean不能被其他bean使用。 
接下来将测试类——SpringTest.java的代码改为:

public class SpringTest {@Testpublic void test() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); ctx.close(); // 正常关闭Spring容器 } }

测试test()方法,将发现Eclipse控制台打印: 

接下来,我们将讲解基本类型对象的依赖注入。

Spring装配基本属性

首先我们将PersonServiceBean类的代码改为:

public class PersonServiceBean implements PersonService { private PersonDao personDao; private String name; private Integer id; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { System.out.println("id = " + id + ", name = " + name); personDao.add(); } }

接着我们要将Spring的配置文件的内容改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 依赖注入的第一种方式 --> <!-- <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="personDao" ref="personDao"></property> </bean> --> <!-- 依赖注入第二种方式: --> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="personDao"> <bean class="cn.itcast.dao.impl.PersonDaoBean"></bean> </property> <!-- 为基本类型属性注入值 --> <property name="name" value="itcast" /> <property name="id" value="88" /> </bean> </beans>

最后测试SpringTest类的test()方法,将发现Eclipse控制台打印: 

到这里,自然就会产生一个疑问——Spring内部是如何注入基本类型对象的呢?有了疑问,我们接下来就来深入剖析其中的原理。

编码剖析Spring装配基本属性的原理

我们应确保Spring的配置文件——beans.xml的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 依赖注入的第一种方式 --> <!-- <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="personDao" ref="personDao"></property> </bean> --> <!-- 依赖注入第二种方式: --> <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="personDao" ref="personDao"></property> <!-- 为基本类型属性注入值 --> <property name="name" value="itcast" /> <property name="id" value="88" /> </bean> </beans>

对照以上Spring的配置文件的内容,我们应该修改PropertyDefinition类的代码为:

/*** 该JavaBean专门用户存放<property ...>的信息* @author li ayun**/
public class PropertyDefinition {private String name;private String ref; private String value; public PropertyDefinition(String name, String ref, String value) { this.name = name; this.ref = ref; this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } }
  • 1

接下来我们就要对传智播客版的Spring容器修修改改了,由于我们要将把本身为字符串的值转成相应的属性类型的值,所以就要用到commons-beanutils工具,即要将commons-beanutils-1.9.2.jar包导入到项目中去。这样,ItcastClassPathXMLApplicationContext类的代码就应该为:

/*** 传智播客版Spring容器* @author li ayun**/
public class ItcastClassPathXMLApplicationContext { private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons = new HashMap<String, Object>(); public ItcastClassPathXMLApplicationContext(String filename) { this.readXML(filename); this.instanceBeans(); this.injectObject(); } /** * 为bean对象的属性(依赖)注入值 */ private void injectObject() { for (BeanDefinition beanDefinition : beanDefines) { Object bean = sigletons.get(beanDefinition.getId()); if (bean != null) { try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for (PropertyDefinition propertyDefinition : beanDefinition.getPropertys()) { for (PropertyDescriptor propertyDesc : ps) { if (propertyDefinition.getName().equals(propertyDesc.getName())) { Method setter = propertyDesc.getWriteMethod(); // 获取属性的setter方法,若setter方法是private的 if (setter != null) { // 最好判断有无setter方法,因为属性可以没有setter方法 Object value = null; if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim()) ) { value = sigletons.get(propertyDefinition.getRef()); } else { // 注入基本类型 value = ConvertUtils.convert(propertyDefinition.getValue(), propertyDesc.getPropertyType()); // 把本身为字符串的值转成相应的属性类型的值 } setter.setAccessible(true); // 允许访问私有的setter方法 setter.invoke(bean, value); // 把引用对象注入到属性中 } break; } } } } catch (Exception e) { e.printStackTrace(); } } } } /** * 完成bean的实例化 */ private void instanceBeans() { for (BeanDefinition beanDefinition : beanDefines) { try { if (beanDefinition.getClassName() != null && !"".equals(beanDefinition.getClassName().trim())) { sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } } catch (Exception e) { e.printStackTrace(); } } } /** * 读取XML配置文件 * @param filename */ private void readXML(String filename) { SAXReader saxReader = new SAXReader(); Document document = null; try { URL xmlpath = this.getClass().getClassLoader().getResource(filename); document = saxReader.read(xmlpath); Map<String, String> nsMap = new HashMap<String, String>(); nsMap.put("ns", "http://www.springframework.org/schema/beans"); // 加入命名空间 XPath xsub = document.createXPath("//ns:beans/ns:bean"); // 创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap); // 设置命名空间 List<Element> beans = xsub.selectNodes(document); // 获取文档下所有bean节点 for (Element element : beans) { String id = element.attributeValue("id"); // 获取id属性值 String clazz = element.attributeValue("class"); // 获取class属性值 BeanDefinition beanDefine = new BeanDefinition(id, clazz); XPath propertysub = element.createXPath("ns:property"); propertysub.setNamespaceURIs(nsMap); // 设置命名空间 List<Element> propertys = propertysub.selectNodes(element); for (Element property : propertys) { String propertyName = property.attributeValue("name"); String propertyRef = property.attributeValue("ref"); // System.out.println(propertyName + "=" + propertyRef); // 测试用 String propertyValue = property.attributeValue("value"); PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyRef, propertyValue); beanDefine.getPropertys().add(propertyDefinition); } beanDefines.add(beanDefine); } } catch (Exception e) { e.printStackTrace(); } } /** * 获取bean实例 * @param beanName * @return */ public Object getBean(String beanName) { return this.sigletons.get(beanName); } }
  • 1

最后,我们就要对传智播客版的Spring容器测试一把了,看它是否真能如我们所想的那样完成对基本类型对象的注入。我们修改单元测试SpringTest类的代码为:

public class SpringTest {@Testpublic void test() { ItcastClassPathXMLApplicationContext ctx = new ItcastClassPathXMLApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); } }

测试test(),可看到Eclipse控制台打印: 
 
这就已说明模拟Spring容器成功了,我们也对其中的原理有了更深刻的认识。如要查看源码,可点击编码剖析Spring装配基本属性的原理进行下载。

转载于:https://www.cnblogs.com/telwanggs/p/6913157.html

(转)编码剖析Spring装配基本属性的原理相关推荐

  1. (转)编码剖析Spring依赖注入的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...

  2. (转)编码剖析Spring管理Bean的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...

  3. (转)编码剖析@Resource注解的实现原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52860046 上文我们已经学会使用@Resource注解注入属性.学是学会了,但也仅限于会使用 ...

  4. 深入剖析Spring架构与设计原理(一)

    深入剖析Spring架构与设计原理(一) 1. IOC的基础 IOC/AOP是Spring的基础, 是Spring平台实现的核心部分. 虽然,我们一开始大多只是在这个层面上,做一些配置和外部特性的使用 ...

  5. 3.了解Spring Boot2自动配置原理

    了解Spring Boot2自动配置原理 1.SpringBoot特点 1.1.依赖管理 1.父项目做依赖管理 依赖管理 <parent><groupId>org.spring ...

  6. spring security加载原理(基于springboot)

    一.基本架构 二.自动配置原理 依据 Spring Boot 自动配置原理,其会自动加载spring-boot-autoconfigure.jar中/META-INF/spring.factories ...

  7. 万字长文剖析清楚 Go 语言 defer 原理

    大纲 编译器怎么编译 defer `struct _defer` 数据结构 `struct _defer` 内存分配 执行 defer 函数链( `deferreturn`  ) defer 怎么传递 ...

  8. Spring Boot进阶:原理、实战与面试题分析

    在当下的互联网应用中,业务体系日益复杂,业务功能也在不断地变化.以典型的电商类应用为例,其背后的业务功能复杂度以及快速迭代要求的开发速度,与5年前的同类业务系统相比,面临着诸多新的挑战. 这些挑战中核 ...

  9. 学习第三篇:【SpringBoot-Labs】芋道 Spring Boot 自动配置原理

    本周(8.21-8.27)将学习芋道 Spring Boot的以下文章: 8.21: 快速入门 8.22:Spring Boot 自动配置原理 .Jar 启动原理 8.23:调试环境. 热部署入门.消 ...

最新文章

  1. docker搭建ssr
  2. 设计模式09: Decorator 装饰模式(结构型模式)
  3. Spring Boot 内置Tomcat——IntelliJ IDEA中配置模块目录设为文档根目录(DocumentRoot)解决方案
  4. Oracle:select 或 inactive 会话语句产生锁?
  5. jquery(js) 增加 删除 修改属性样式、元素内容
  6. ArcGIS API for Silverlight 学习笔记(1)
  7. AttributeError: module 'sys' has no attribute 'maxint'
  8. java 链表逆转_java 实现单链表逆转详解及实例代码
  9. 实现图片大小的自动控制( 图片大小控制CSS代码)
  10. Python pip的使用
  11. java 避免重定向_java – 避免循环重定向使用HttpClient 4.1.1
  12. 分子动力学模拟需要掌握的理论、语言和软件
  13. # 冰冻三尺非一日之寒。
  14. ept技术_intel EPT 机制详解
  15. 转://Oracle 11gR2 硬件导致重新添加节点
  16. java 生僻字 问号_csv导出姓名生僻字变问号
  17. sublime text3 英文版转为中文版
  18. 明翰经验系列之人生经验与经典文案篇(持续更新)V1.1
  19. 一个球从100米高度自由下落,每次落地后反跳回原来的高度的一半,再落下;
  20. Latex之添加删除线

热门文章

  1. JDK1.8 IdentityHashMap
  2. (31)System Verilog类class详解
  3. Verilog实现2分频实例
  4. div iframe html5,深入理解iframe
  5. Linux内核分析 - 网络[十四]:IP选项
  6. TLS 1.2 握手过程
  7. html里面textfield属性,StyleableTextField的CSS属性htmlText
  8. 【LeetCode】剑指 Offer 22. 链表中倒数第k个节点
  9. Java的四种引用方式
  10. finalize方法注意事项(防止子类没有显示调用super.finalize())