关于spring依赖注入原理的文章在网络上已经有很多,我要写的这篇文章原文出自http://taeky.iteye.com/blog/563450,只所以再一次写下来只是为了一为自己收藏,方便以后的复习;二是自己在用这个例子实践的时候遇到一些问题,自己记一下。另外这篇文章中用dom4j来解析spring的配置文件,我觉得也是个值得学习的地方。好了,不多说了,还是把代码再贴一下:
接口:PersonDao.java
package com.luojing.test.dao;
public interface PersonDao {
public void add();
}
实现类:PersonDaoImp.java
package com.luojing.test.daoimp;
import com.luojing.test.dao.PersonDao;
public class PersonDaoImp implements PersonDao {
public void add() {
System.out.println("执行了add()方法!!!");
}
}
服务接口:PersonService.java
package com.luojing.test.service;
public interface PersonService {
public void save();
}
服务实现类:PersonServiceImp.java
package com.luojing.test.serviceimp;
import com.luojing.test.dao.PersonDao;
import com.luojing.test.service.PersonService;
public class PersonServiceImp implements PersonService {
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void save() {
personDao.add();
}
}
applicationContext.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-2.5.xsd" >
<bean id="personDao" class="com.luojing.test.daoimp.PersonDaoImp"/>
<bean id="service" class="com.luojing.test.serviceimp.PersonServiceImp">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>
创建bean,然后其属性用一集合存储在bean里BeanDefinition.java:
package com.luojing.test.testioc;
import java.util.ArrayList;
import java.util.List;
public class BeanDefinition {
private String id;
private String classname;
private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>();
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;
}
public BeanDefinition(String id,String classname){
this.id = id;
this.classname = classname;
}
}
属性bean PropertyDefinition.java:
package com.luojing.test.testioc;
public class PropertyDefinition {
private String name;
private 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;
}
public PropertyDefinition(String name,String ref){
this.name = name;
this.ref = ref;
}
}
解析spring配置文件并实例话bean ItcastClassPathXMLApplicationContext.java:
package com.luojing.test.testioc;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
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 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) {
// 通过反射技术把bean都创建出来
e.printStackTrace();
}
}
}
/**
* 为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();
//Introspector通过这个类可以取得bean的定义信息
for (PropertyDefinition propertyDefinition : beanDefinition
.getPropertys()) {
for (PropertyDescriptor properdesc : ps) {
if (propertyDefinition.getName().equals(properdesc.getName())) {
Method setter = properdesc.getWriteMethod();// 获取属性的setter方法
// ,private
if (setter != null) {//属性可能没有set方法,所以这里要判断一下
Object value = sigletons
.get(propertyDefinition.getRef());
setter.setAccessible(true);//如果set方法是私有的话,要设置它允许被访问
setter.invoke(bean, value);// 把引用对象注入到属性
}
break;
}
}
}
} catch (Exception e) {
}
}
}
}
/**
* 读取xml配置文件
*/
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属性值
System.out.println(id + " = " + clazz);
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实例
*/
public Object getBean(String beanName) {
return this.sigletons.get(beanName);
}
}
测试类SpringTest.java:
我在运行的过程中出现:java.lang.NoClassDefFoundError: org/jaxen/JaxenException弄了我好半天,后来在网上找出其原因是:除去必须有一个dom4j.jar外,还必须有一个jaxen-1.1.1.jar 文件,因为使用dom4j时调用了XPath,而没有在项目中加载jaxen-xx.xx.jar,jaxen是一个用Java开发的XPath 引擎,支持JDOM, dom4j。