cxt.close();
---------------------------------------------------------beans.xml-----------------------------------------------
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PersonService;
import service.impl.PersonServiceImpl;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
ItCastClassPathXmlApplicationContext ctx=new ItCastClassPathXmlApplicationContext("beans.xml");
/*PersonService ps=(PersonServiceImpl) ctx.getBean("personService");
PersonService ps1=(PersonServiceImpl) ctx.getBean("personService");
System.out.println(ps==ps1);
ps.save();*/
PersonService ps=(PersonServiceImpl) ctx.getBean("ss");
ps.save();
}
}
----------------------------------------------------BeanDefinition.java---------------------------------------------
package junit.test;
import java.util.ArrayList;
import java.util.List;
public class BeanDefinition {
private String id;
private String clazz;
private List properties=new ArrayList();
public BeanDefinition(){}
public BeanDefinition(String id,String clazz){
this.id=id;
this.clazz=clazz;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public List getProperties() {
return properties;
}
public void setProperties(List properties) {
this.properties = properties;
}
}
package junit.test;
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) {
super();
this.name = name;
this.ref = ref;
}
}
package junit.test;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Method;
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 beanDefines=new ArrayList();
private Map sigletons=new HashMap();
public ItCastClassPathXmlApplicationContext(String filename){
this.readXml(filename);
this.instanceBeans();
this.injectBeans();
}
/**
* 为bean对象注入值。
*
* @description
* @return void
*/
private void injectBeans() {
for(BeanDefinition beanDefinetion :beanDefines){
Object bean=sigletons.get(beanDefinetion.getId());
if(bean!=null){
try {
PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDefinition propertyDefinition:beanDefinetion.getProperties()){
for(PropertyDescriptor propertydesc:ps){
if(propertyDefinition.getName().equals(propertydesc.getName())){
Method setter=propertydesc.getWriteMethod();
Object value=sigletons.get(propertyDefinition.getRef());
if(setter!=null){
setter.setAccessible(true);
setter.invoke(bean, value);
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 完成bean实例化
*/
private void instanceBeans(){
for(BeanDefinition beanDefine:beanDefines){
try {
sigletons.put(beanDefine.getId(),Class.forName(beanDefine.getClazz()).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* 用于读取配置文件
*/
private void readXml(String filename) {
SAXReader saxReader=new SAXReader();
Document document=null;
try{
//URL xmlpath=this.getClass().getClassLoader().getResource(filename);
document=saxReader.read(new File(filename));
Map nsMap=new HashMap();
nsMap.put("ns", "http://www.springframework.org/schema/beans");//加入命名空间
XPath xsub=document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);//设置命名空间
List 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 properties=propertysub.selectNodes(element);
for(Element property:properties){
String name=property.attributeValue("name");
String ref=property.attributeValue("ref");
System.out.println(name+"="+ref);
PropertyDefinition propertyDefinition=new PropertyDefinition(name,ref);
beanDefine.getProperties().add(propertyDefinition);
}
beanDefines.add(beanDefine);
}
}catch(Exception e){
e.printStackTrace();
}
}
public Object getBean(String beanName){
return sigletons.get(beanName);
}
}