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

Spring的依赖注入

前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中。 
Spring的依赖注入有两种方式:

  • 通过构造器参数,让容器把创建好的依赖对象注入。
  • 使用setter方法进行注入。

现在我们使用第二种方式进行依赖注入。以Spring管理的Bean的生命周期的案例为基础展开本文的说明。 
首先在src目录下新建一个cn.itcast.dao包,并在该包下新建一个接口——PersonDao.java,其代码为:

public interface PersonDao {void add();}
  • 1

提示:在Spring开发中建议大家使用面向接口编程,若我们要实现软件各层之间的解耦,须通过接口。 
紧接着在src目录下新建一个cn.itcast.dao.impl包,并在该包下新建一个PersonDao接口的实现类——PersonDaoBean.java,其代码为:

public class PersonDaoBean implements PersonDao { @Override public void add() { System.out.println("执行PersonDaoBean中的add()方法"); } }
  • 1

然后为了让PersonDaoBean依赖对象注入到PersonServiceBean对象中,我们使用setter方法进行注入,所以要将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService { private PersonDao personDao; public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } @Override public void save() { personDao.add(); } }
  • 1

注意:千万不能忘却setter方法。从上可看出业务逻辑层与数据访问层之间彻底解耦了,这正是我们所想要的。 
再接下来就要修改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> </beans>
  • 1

最后,我们要修改测试类——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容器 } }
  • 1

测试test()方法,Eclipse控制台会打印: 

编码剖析Spring依赖注入的原理

在上面我们使用Spring的依赖注入将PersonDaoBean依赖对象注入到了PersonServiceBean对象中,我们不仅要问Spring依赖注入的内部原理是什么。现在我们就来编码剖析Spring依赖注入的原理。 
首先在junit.test包下新建一个JavaBean——PropertyDefinition.java,该JavaBean专门用于存放<property ...>的信息,其代码如下:

/*** 该JavaBean专门用户存放<property ...>的信息* @author li ayun*/
public class PropertyDefinition { private String name; private String ref; public PropertyDefinition(String name, String ref) { this.name = name; this.ref = ref; } 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

然后我们就要修改BeanDefinition类的代码为:

/*** 将读取到的bean的信息存到一个JavaBean对象中* @author li ayun**/
public class BeanDefinition { private String id; private String className; private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>(); public BeanDefinition(String id, String className) { this.id = id; this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<PropertyDefinition> getPropertys() { return propertys; } public void setPropertys(List<PropertyDefinition> propertys) { this.propertys = propertys; } }
  • 1

紧接着我们就要对传智播客版的Spring容器进行修改了,我们先修改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(); } /** * 完成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); // 测试用 PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyRef); 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

写了这么多代码心里不禁打颤,到底是否正确呢?我们可以先测试一下,将SpringTest测试类的代码置为:

public class SpringTest {@Testpublic void test() { ItcastClassPathXMLApplicationContext ctx = new ItcastClassPathXMLApplicationContext("beans.xml"); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

测试test()方法,发现Eclipse控制台输出:

personDao=personDao

这就说明到目前为止,我们所写的代码是正确无误的。 
接下来,我们就要为bean对象的属性(依赖)注入值了。我们最后一次修改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 = sigletons.get(propertyDefinition.getRef()); 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); // 测试用 PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyRef); 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容器的传智播客版Spring容器就应运而生了。最后,我们就要来测试该传智播客版Spring容器是否可行?只须修改SpringTest测试类的代码为:

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

再次测试test()方法,Eclipse控制台输出:

执行PersonDaoBean中的add()方法

通过编写代码剖析Spring依赖注入的原理,我们就能比别人更加深刻地理解Spring内部到底是如何实现依赖注入的。源码可点击编码剖析Spring依赖注入的原理进行下载。

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

(转)编码剖析Spring依赖注入的原理相关推荐

  1. (转)编码剖析Spring装配基本属性的原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52856465 上回我们已经讲到了Spring依赖注入的第一种方式,现在我们来详解第二种方式,须 ...

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

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

  3. spring依赖注入底层原理与源码分析

    Spring中有几种依赖注入方式? 1.手动注入-set方法注入和构造器注入 2.自动注入-@Autowired注解和xml注入 autowrire参数: no 默认不开启 byName 根据被注入属 ...

  4. Java程序员进阶——Spring依赖注入原理分析

    Spring依赖注入原理分析 下面谈谈Spring是如何实现反转模式IOC或依赖注入模式DI: 平时,我们需要生成一个对象,使用new语法,如一个类为A public class A{public v ...

  5. spring依赖注入原理(转载)

    关于spring依赖注入原理的文章在网络上已经有很多,我要写的这篇文章原文出自http://taeky.iteye.com/blog/563450,只所以再一次写下来只是为了一为自己收藏,方便以后的复 ...

  6. spring依赖注入原理详解(转载)

    spring依赖注入原理详解----转载 所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中.当spring容器启动后,spring容器初始化,创建并管理bean对象,以及销毁它.所 ...

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

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

  8. diy实现spring依赖注入

    [README] 本文diy代码实现了 spring 依赖注入,一定程度上揭示了依赖注入原理: [1]控制反转-Inversion of Control 是一种编码思想,简而言之就是 应用程序A可以使 ...

  9. 什么是DI(依赖注入),依赖注入的原理

    这篇文章主要介绍了Spring当中的依赖注入(DI),以及他能注入的数据类型,和他的简单原理,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 什么是依赖注入? D ...

最新文章

  1. Eclipse搭建Mybatis框架
  2. esp8266 扫描wifi_基于ESP8266与QT的智能家居控制系统设计
  3. 哪些情况需用到数据分析工具
  4. Linq专题之提高编码效率—— 第三篇 你需要知道的枚举类
  5. proxy_redirect参数的作用
  6. Filenet公布首批候选自治社区
  7. ISMS信息安全管理体系与信息系统安全等级保护标准的异同点
  8. 曹金明:Zynga大败局--数据控是如何把游戏做败的
  9. pdm系统是归档服务器吗,PDM系统的主要功能
  10. FaceBoxes: A CPU Real-time Face Detector with High Accuracy
  11. 阿里巴巴食堂:看菜名我跪了~程序员:给我来个“油炸产品经理”
  12. Landesk学习笔记1_Landesk三种拖送方式
  13. 关于GPL协议的理解(开源与商用、免费与收费的理解)
  14. Merriam-Webster's Vocabulary Builder 学习笔记 Unit 5
  15. IB课程必修课TOK到底有啥用?
  16. 华为7.31笔试(第一题AC、第二题AC、第三题超纲)
  17. Android完全禁止第三方软件安装的方法
  18. 统计学考研和计算机考研真题,20考研北大数院统计/叉院数据科学(统计学)备考经验...
  19. AD转换器的参数介绍
  20. 【安全知识分享】安全生产月活动总结课件(附下载)

热门文章

  1. (76)译码器与编码器(三八译码器)
  2. (12)System Verilog随机变量
  3. (71)Verilog HDL时间度量系统函数:$time
  4. mqtt server python_使用python实现mqtt的发布和订阅
  5. python制作一个简单的udp聊天器
  6. 服务器与HTML客户端通信,服务器与HTML客户端通信
  7. 基于Flash的ECC纠错算法基本原理及软件C语言算法和硬件Verilog实现(PPT在主页可下载)
  8. tems软件语音测试模板,测试软件使用-TEMS.ppt
  9. 【实验1】——脉冲测距
  10. 互联网络层的内核实现[内核中的路由机制]