package junit.test;
public class BeanDefinition {
private String id;
private String className;
public BeanDefinition(String id,String className){
this.id = id;
this.className = 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;
}
}
package junit.test;
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> singletons = new HashMap<String , Object>();
public ItcastClassPathXMLApplicationContext(String filename){
this.readXML(filename);
this.instanceBeans();
}
/**
* 完成bean實例化
*/
private void instanceBeans() {
for(BeanDefinition beanDefinition:beanDefines){
try {
if(beanDefinition.getClassName()!=null&&!"".equals(beanDefinition.getClassName().trim()))
singletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
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);//獲取文檔下所有bean節點
for(Element element:beans){
String id = element.attributeValue("id");//獲取id屬性
String clazz = element.attributeValue("class");//獲取class屬性
BeanDefinition beanDefine = new BeanDefinition(id,clazz);
beanDefines.add(beanDefine);
}
}catch(Exception e){
e.printStackTrace();
}
}
public Object getBean(String beanName){
return this.singletons.get(beanName);
}
}
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.itcast.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring(){
ItcastClassPathXMLApplicationContext ctx = new ItcastClassPathXMLApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save();
}
}
package cn.itcast.service;
public interface PersonService {
public abstract void save();
}
package cn.itcast.service.impl;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
/* (non-Javadoc)
* @see cn.itcast.service.impl.PersonService#save()
*/
public void save(){
System.out.println("Iam save()");
}
}
<?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="personService" class="cn.itcast.service.impl.PersonServiceBean" >
</bean>
</beans>