spring的基础(一)自己写的ClassPathXmlApplicationContext类
[/align][align=center]package org.service.base;
import java.beans.IntrospectionException;
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.apache.commons.beanutils.ConvertUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class CuiRanClassPathXmlApplicationContext {
private List<BeanDefinition> beanDefines=new ArrayList<BeanDefinition>();
private Map<String, Object> sigletons=new HashMap<String, Object>();
public CuiRanClassPathXmlApplicationContext(String filename){
this.readXML(filename);
this.instanceBeans();
this.injectObject();
}
/**
* 为bean对象注入值
*/
private void injectObject() {
// TODO Auto-generated method stub
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 properdesc:ps){
if(propertyDefinition.getName().equals(properdesc.getName())){
Method setter=properdesc.getWriteMethod();//获取属性的setter方法,private
if(setter!=null){
Object value=null;
if(propertyDefinition.getRef()!=null&&!"".equals(propertyDefinition.getRef().trim()))
{
value=sigletons.get(propertyDefinition.getRef());
}else{
value=ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType());
}
setter.setAccessible(true);
setter.invoke(bean, value);//把引用对象注入到属性中
}
break;
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 完成bean的实例化
*/
private void instanceBeans() {
// TODO Auto-generated method stub
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) {
// TODO: handle exception
e.printStackTrace();
}
}
}
/**
* 读取xml配置文件
* @param filename
*/
private void readXML(String filename) {
// TODO Auto-generated method stub
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);//获得文档下所有节点
for(Element element:beans){
String id=element.attributeValue("id");//获取ID属性值
String clazz=element.attributeValue("class");//获取class属性值
BeanDefinition beanDefine=new BeanDefinition(id,clazz);
XPath propertyxsub=element.createXPath("ns:property");
propertyxsub.setNamespaceURIs(nsMap);
List<Element> propertys=propertyxsub.selectNodes(element);
for(Element property:propertys){
String propertyName=property.attributeValue("name");
String propertyRef=property.attributeValue("ref");
String propertyValue=property.attributeValue("value");
System.out.println(propertyName+":"+propertyRef);
PropertyDefinition propertyDefinition=new PropertyDefinition(propertyName,propertyRef,propertyValue);
beanDefine.getPropertys().add(propertyDefinition);
}
beanDefines.add(beanDefine);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 获取bean实例
* @param beanName
* @return
*/
public Object getBean(String beanName){
return this.sigletons.get(beanName);
}
}