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. can't set android permissions - built without android support

    /**************************************************************************** * can't set android pe ...

  2. SPOJ:Lexicographically Smallest(并查集&排序)

    Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...

  3. SPOJ:Stack Overflow(并查集)

    Stack is a basic data structure. Where 3 operation can be done- Push: You can push object to the sta ...

  4. 【POI 2006】 Tet-Tetris-3D

    [题目链接] 点击打开链接 [算法] 二维线段树(树套树) 注意标记永久化 [代码] #include<bits/stdc++.h> using namespace std; #defin ...

  5. web.xml配置之<context-param>

    <context-param>的作用和用法: 1.<context-param>配置是是一组键值对,比如: <context-param>        <p ...

  6. Spring中Bean获取IOC容器服务的方法

    Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...

  7. c/c++内存机制(一)(原)

    一:C语言中的内存机制 在C语言中,内存主要分为如下5个存储区: (1)栈(Stack):位于函数内的局部变量(包括函数实参),由编译器负责分配释放,函数结束,栈变量失效. (2)堆(Heap):由程 ...

  8. bzoj1087互不侵犯King——状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1087 水题... 然而犯了两个致命小错误,调了好半天...详见注释. 代码如下: #incl ...

  9. registerWithTouchDispatcher 注册单点触摸事件

    Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...

  10. git操作实战指南

    1 背景 小白进入公司,进入日常多人开发,git的使用应该是新人要掌握的第一个技能.git是一个分布式数据存储库,分为远程存储和本地存储,本地存储的话,每一台计算机就相当于一个存储数据库,可以记录和存 ...