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 {

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

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

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

(转)编码剖析Spring装配基本属性的原理的更多相关文章

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

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

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

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

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

    http://blog.csdn.net/yerenyuan_pku/article/details/52834561 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. (转)编码剖析@Resource注解的实现原理

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

  8. Spring(八)编码剖析@Resource注解的实现原理

    配置文件beans2.xml <?xml version="1.0" encoding="UTF-8"? > <beans xmlns=&qu ...

  9. Spring2.5学习3.2_编码剖析@Resource注解的实现原理

    首先看一下J2EE提供的@Resource注解:该注解默认安照名称进行装配,名称能够通过name属性进行指定, 假设没有指定name属性,当注解写在字段上时,默认取字段名进行依照名称查找,假设注解写在 ...

随机推荐

  1. 跨平台实现zip压缩加密功能

    使用zlib将文件夹压缩成zip文件时,需要自己读取文件然后写入zip文件.利用官方下载的zlib包中包含的contrib/minizip/zip.h和zip.c代码提供的函数,可以很容易实现这个功能 ...

  2. bzoj 3653 谈笑风生 —— 主席树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3653 对于一个 (a,b,c),分成 b 是 a 的祖先和 b 在 a 子树里两部分: 第一 ...

  3. Linux系统之文件传输的几种方式

    Linux系统安装好以后以及能上网.能进行软件安装后,接下来可能就需要从其它机器复制一些文件或者把文件复制到其它机器,那么就涉及到文件的传输和共享,下面介绍一下常规的一些文件传输和共享方案. 1.传统 ...

  4. vs2008控制台程序运行一闪而过,不显示按任意键继续

    调试运行(F5)而且没插断点.且程序没有暂停的点,就会一闪而过 直接执行(Ctrl+F5),在程序结束前会有“按任意键继续” 当你不想进入调试状态,只想看一看程序执行结果时用ctrl+f5F5会进入调 ...

  5. 安装并配置JAVA环境

    详见百度经验 http://jingyan.baidu.com/article/0202781175839b1bcc9ce529.html

  6. 10.22~10.28一周经典题目整理(meeting,BZOJ4377,POJ3659)

    meeting:给正n边形每个点染上黑色或者白色,问有多少个同色的等腰三角形. 以正五边形为例这里将最上面的点作为顶点,得到若干对相等的腰 ,注意到以最上面的点作为顶点的等腰三角形的个数,等于颜色相等 ...

  7. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 17. 基于Claim和Policy的授权 上

    首先补一下昨天没有讲的东西 只有管理员才能访问UserController RoleController都加上这个角色 Cliam 不是管理员角色的用户访问 cliam是name个Value值的键值对 ...

  8. 任务25:IHostEnvironment和 IApplicationLifetime介绍

    任务25:IHostEnvironment和 IApplicationLifetime介绍 IHostingEnvironment这个里面有一些参数,比如我们当前应用程序的名称.目录的. await ...

  9. HK算法模板+小优化(跑的快一点点)

    HUST 2604 #include <iostream> #include <cstdlib> #include <cstdio> #include <cs ...

  10. iOS UITextView 设置圆角边框线

    textView.layer.borderColor = UIColor.lightGray.cgColor textView.layer.cornerRadius = 4 textView.laye ...