转载请注意出处:http://blog.csdn.net/zcm101
写在前面的话
最近,给项目组成员培训了Spring 控制反转和依赖注入的原理,并自己做了个Lazy Coder版的Spring,现在给大家分享下,相互学习,有说得不对的欢迎指正。
由于代码较多,分成两篇博文来写,上一篇文章讲了下Spring的基本配置,这次我们来看看如何制作一个Lazy Coder版的Spring。
另:我们是使用xml配置来管理bean,并没有用到注释。
思考一个问题
我们要写一个类似Lazy Coder版的Spring,那么我们写出来是个框架还是工具呢?框架和工具有什么区别?
思路
1. 我们要写Lazy Coder版的Spring,那么在应用Spring开发过程中,哪些可以保留,哪些需要被代替?
2. Spring在开发过程中,需要有几样东西:Jar,配置文件,业务类。既然是Lazy Coder版,那spring的Jar肯定不能用了,那么能要保留的就是配置文件,业务类。
3. 既然有xml配置文件,那么我们就要用到读xml的包,跟spring一样,我们用dom4j.jar,在spring文件夹下可以找到。
4. Spring是个容器,负责业务bean的实例化,那么我们的Lazy Coder也是个容器,也要实例化容器,在3中我们读到了业务bean的配置,全是字符串,利用反射我们就可以得到类,并实例化。
5. 经过前面4步后,我们的容器就拥有所有的业务bean实例了。接下来就是注入了,注入的原理也很简单,就是利用反射,动态的调用类中的set方法。
先来看看我们最终的项目图:
IOC实现
1. 我们准备新建一个类,叫LazycoderClassPathXmlApplicationContext,这就是我们的容器类了。在上面第3点中,我们提到要读xml,那么读完的xml这么多字符串放到哪呢?java是个面向对象的语言,我们得把一个一个的bean配置,变成一个一个的类,所以我们新建一个类Bean,里面和xml一样,只放两个属性:id, className(注意,xml中配置的class属性值是类名的全路径,为了和java里的class区分,所以我们叫className)
package com.lazycoder.spring.ioc;
public class Bean {
public Bean(String id, String className){
this.id = id;
this.className = className;
}
private String id;
private String 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;
}
}
有了Bean后,我们的容器就得定义一个集合来存这些Bean
public List<Bean> beans = new ArrayList<Bean>();
然后就可以读xml了。读xml的方法就不具体介绍了,不会请问百度。所以容器实例化后,第一个动作:readXml()。
2. 将配置文件转成java bean后,就可以开始实例化了,所以我们要遍历上面定义过的beans,使用Class.forName(bean.getClassName()).newInstance()来实例化,那么实例化后的对象放在哪呢?我们得再建一个集合来放这些对象
public Map<String, Object> map = new HashMap<String, Object>();
第二个动作:instanceBeans().
3. 实例化后的Bean有地方存后,总得有地方取吧,所以容器中应该有getBean(String key)方法。
4. 经过上面的分析后,来看下我们的容器长什么样吧
package com.lazycoder.spring.ioc;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class LazycoderClassPathXmlApplicationContext {
/**
* 存储配置文件中的Bean节点
* 将bean节点变成Bean对象
*/
public List<Bean> beans = new ArrayList<Bean>();
/**
* 存储实例化对象的容器
* key = bean节点里的id属性值
* value = bean节点里的class属性值,利用Class.forName("className").newInstance()实例化出来的对象
*/
public Map<String, Object> map = new HashMap<String, Object>();
public LazycoderClassPathXmlApplicationContext(String configPath){
readXml(configPath);
instanceBeans();
}
/**
* 读取配置文件,将xml配置变成bean对象
* @param configPath
*/
@SuppressWarnings("rawtypes")
private void readXml(String configPath){
SAXReader saxReader = new SAXReader();
Document document;
URL xmlPath = LazycoderClassPathXmlApplicationContext.class.getClassLoader().getResource(configPath);
try {
document = saxReader.read(xmlPath);
Element root = document.getRootElement();
//循环bean节点
for(Iterator iterator = root.elementIterator("bean");iterator.hasNext();){
Element element = (Element) iterator.next();
String id = element.attributeValue("id");
String className = element.attributeValue("class");
beans.add(new Bean(id, className));
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 实例化所有的bean
*/
public void instanceBeans(){
for(Bean bean : beans){
try {
map.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 从容器中获取bean实例
* @param id
* @return
*/
public Object getBean(String id){
return map.get(id);
}
}
配置文件及业务service我们就不给了,直接来看我们的测试代码
LazycoderClassPathXmlApplicationContext context = new LazycoderClassPathXmlApplicationContext("spring.xml");
PersonService personService = (PersonService) context.getBean("personService");
personService.save();
这里我们已经脱离了Spring的ClassPathXmlApplicationContext,完全是用Lazy Coder版的,实践证明成功了。
DI实现
DI的实现必须依赖IOC,也就是说,必须将所有的类交给Spring来管理,Spring才知道要注入的是哪个类,注入的源类和目标类都必须交由Spring管理。
1. 分析配置文件中,比原来多了些Property节点,和bean的分析方法一样,需要新建个Property类,内有ref和name两个属性,Bean类也要加点东西,需要定义一个List,用来存放多个Property
package com.lazycoder.spring.di;
public class Property {
public Property(String name, String ref) {
this.name = name;
this.ref = ref;
}
public String name;
public String 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;
}
}
package com.lazycoder.spring.di;
import java.util.List;
public class Bean {
public Bean(String id, String className, List<Property> properties) {
super();
this.id = id;
this.className = className;
Properties = properties;
}
private String id;
private String className;
private List<Property> Properties;
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<Property> getProperties() {
return Properties;
}
public void setProperties(List<Property> properties) {
Properties = properties;
}
}
2. 我们读xml的方法也得做修改了,取了循环读bean节点外,还要嵌套循环读取property节点,代码在后面给出。
3. 实例化,跟IOC实现中的实例化一样,无需修改。
4. 第三个动作:注入
注入的原理,就是调用业务Service类中的setXXXDao方法,所以要先循环容器中的对象object,看是否有property节点,有的话得到其name值,再利用反射得到类所有的方法object.getClass().getDeclaredMethods(),再遍历类的所有方法,找到要注入的set方法(规则:set+name值首字母大写),找到后,再用ref值到IOC容器中找到dao对象,之后就用method.invoke(object, object2);就可以在代码中动态调用set方法了,注入完成。
描述比较复杂,我语言表达能力有限,没看懂的得多看几遍了,或者直接看代码吧。
package com.lazycoder.spring.di;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class LazycoderClassPathXmlApplicationContext {
/**
* 存储配置文件中的Bean节点
*/
public List<Bean> beans = new ArrayList<Bean>();
/**
* 存储实例化对象
*/
public Map<String, Object> map = new HashMap<String, Object>();
public LazycoderClassPathXmlApplicationContext(String configPath){
readXml(configPath);
instanceBeans();
injectBeans();
}
/**
* 读取配置文件,保存到实体中
* @param configPath
*/
@SuppressWarnings("rawtypes")
private void readXml(String configPath){
SAXReader saxReader = new SAXReader();
Document document;
URL xmlPath = LazycoderClassPathXmlApplicationContext.class.getClassLoader().getResource(configPath);
try {
document = saxReader.read(xmlPath);
Element root = document.getRootElement();
//循环bean节点
for(Iterator iterator = root.elementIterator("bean");iterator.hasNext();){
Element element = (Element) iterator.next();
String id = element.attributeValue("id");
String className = element.attributeValue("class");
List<Property> properties = new ArrayList<Property>();
//循环property节点
for(Iterator iteratorProperty = element.elementIterator(); iteratorProperty.hasNext();){
Element elementProperty = (Element) iteratorProperty.next();
String name = elementProperty.attributeValue("name");
String ref = elementProperty.attributeValue("ref");
properties.add(new Property(name, ref));
}
beans.add(new Bean(id, className, properties));
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 实例化所有的bean
*/
public void instanceBeans(){
for(Bean bean : beans){
try {
map.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 从容器中获取bean实例
* @param id
* @return
*/
public Object getBean(String id){
return map.get(id);
}
/**
* 将property节点对应的实例,注入到bean节点实例中
*
*/
public void injectBeans(){
for(Bean bean : beans){
Object object = map.get(bean.getId());
for(Property property : bean.getProperties()){
String fieldName = property.getName();
Method[] methods = object.getClass().getDeclaredMethods();
for(Method method : methods){
//判断方法名是否与属性名匹配性
if(method.getName().equals("set" + fieldName.substring(0,1).toUpperCase() + fieldName.substring(1))){
try {
//调用set方法注入
Object object2 = map.get(property.getRef());
method.invoke(object, object2);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}
}
}
配置文件:
<?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-2.5.xsd">
<bean id="hibernateDao" class="com.lazycoder.dao.impl.HibernateDaoImpl"/>
<bean id="jdbcDao" class="com.lazycoder.dao.impl.JdbcDaoImpl"/>
<bean id="baseService" class="com.lazycoder.service.impl.BaseServiceImpl">
<!-- 通过name属性在baseServiceImpl里找到要注入的set方法,即 SetHibernateDao
通过ref属性在容器中找到对应id的实例化对像,即<bean id="hibernateDao"... -->
<property name="hibernateDao" ref="hibernateDao"/>
<property name="jdbcDao" ref="jdbcDao"/>
</bean>
</beans>
测试代码:
@Test
public void testLazycoder(){
LazycoderClassPathXmlApplicationContext context = new LazycoderClassPathXmlApplicationContext("spring.xml");
BaseService baseService = (BaseService) context.getBean("baseService");
baseService.add();
baseService.update();
}
输出:
BaseServiceImpl add
HibernateDaoImpl add
BaseServiceImpl update
JdbcDaoImpl update
成功了!!至此,我们LazyCoder版的Spring完成了,完全可以代码Spring的控制反转,依赖注入功能。
回答下开篇的问题
我们Lazy Coder版的Spring就是个框架,框架和工具的区别简单的说,工具是写出一堆API供项目调用,框架就是用来调用项目中的代码。