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 {

    @Test
public 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 {

    @Test
public 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 {

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

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

执行PersonDaoBean中的add()方法

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

(转)编码剖析Spring依赖注入的原理的更多相关文章

  1. Spring、Spring依赖注入与编码剖析Spring依赖注入的原理

    Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...

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

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

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

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

  4. Spring、编码剖析Spring管理Bean的原理

    引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...

  5. Spring第三弹—–编码剖析Spring管理Bean的原理

    先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  6. 编码剖析Spring管理bean的原理

    project目录 MyClassPathXMLApplicationContext读取xml,以及实例化bean. 因为是一开始实例化配置文件所有bean,所以需要构造器完成这些工作. packag ...

  7. SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式

           在java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依 ...

  8. Spring依赖注入原理分析

    在分析原理之前我们先回顾下依赖注入的概念: 我们常提起的依赖注入(Dependency Injection)和控制反转(Inversion of Control)是同一个概念.具体含义是:当某个角色( ...

  9. Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理

    一.spring依赖注入使用方式 @Autowired是spring框架提供的实现依赖注入的注解,主要支持在set方法,field,构造函数中完成bean注入,注入方式为通过类型查找bean,即byT ...

随机推荐

  1. robotframework:appium切换webview后,在第一个页面操作成功,跳转到第二个页面后,执行命令失败

    问题: 在用robot写手机淘宝app的自动化时,打开手机淘宝后,点击天猫国际,跳转到天猫国际页面,天猫国际页面是H5, 需要切换到对应的webview,切换到webview后,点击美妆菜单,跳转到美 ...

  2. scp.sh

    #!/bin/sh #Auto change server files #liudong 2016-3-21 if [ ! -f ip.txt ];then echo -e "\033[31 ...

  3. 数据类型总结 (C++)

    数据类型总结 (C++) 作用 C++ 大小 取值范围 后缀 字符 char 1 -128~127   字符(无符号) unsigned char 1 0~255   宽字符 wchar_t 2   ...

  4. 任务41:Individual authentication 模板

    使用命令行创建项目 会自动加上ef core和Identity core代码的示例 默认创建mvc是不带Identity的 dotnet new -help:查看可用的命令: -au:Individu ...

  5. POJ2369【循环节】

    题意: 给一个序列,他需要几步就能变成原来的序列. 思路: 那么就是找一下各个循环节(用dfs随便搞了-),求一下最小公倍数就好了. 贴一发挫code- //#include <bits/std ...

  6. HDU 2063 过山车+poj 1469

    //这是一个非常简单的匹配.其实满感觉这种算法讲道理是可以想到. //但是我们这种弱就只能先学了匈牙利算法,然后随便嗨这种题目了.没事结果都一样. //这就是匹配算法的DFS形式,有一个BFS形式的, ...

  7. 洛谷P4841 城市规划(多项式求逆)

    传送门 这题太珂怕了……如果是我的话完全想不出来…… 题解 //minamoto #include<iostream> #include<cstdio> #include< ...

  8. git 命令参考手册

    你的本地仓库由 git 维护的三棵“树”组成.第一个是你的 工作目录,它持有实际文件:第二个是 缓存区(Index),它像个缓存区域,临时保存你的改动:最后是 HEAD,指向你最近一次提交后的结果. ...

  9. 问题 3: 糖果数(candy)

    问题 3: 糖果数(candy) 题目描述 学校准备去春游,委托小明分发糖果,每位同学一袋,数量随机. 一共有N袋糖果,编号为1到N,小明拿了从编号a袋到编号b袋的糖果去分发,小明想知道,他一共拿了多 ...

  10. RobotFrameWork自动化系列:安装配置

    1.   RobotFrameWork安装配置 1.1. 安装环境 64位win10家庭中文版 网上很多这方面的教程,但是比较零散,这里是自己安装配置的一个简单的笔记. 1.2. 安装说明 由于Rob ...