持久层部分请参考前面hibernate demo一文
services.xml :
模拟spring核心的 BeanPool.java :
接口 Person.java :
服务实例 Chinese.java :
测试类:
services.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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id="daoimpl" class="com.darcy.note.persistance.UserDAOImpl"></bean>
<bean id="chinese" class="com.darcy.note.spring.Chinese">
<property name="name">
<value>小明</value>
</property>
<property name="age">
<value>12</value>
</property>
<property name="list">
<list>
<value>aa</value>
<value>bb</value>
</list>
</property>
<property name="dao">
<ref bean="daoimpl" />
</property>
</bean>
<bean id="american" class="com.darcy.note.spring.American">
<constructor-arg index="0">
<ref bean="daoimpl" />
</constructor-arg>
<property name="sb">
<value>sbbb</value>
</property>
</bean>
</beans>
模拟spring核心的 BeanPool.java :
package com.darcy.note.spring;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class BeanPool {
private String path;
public BeanPool(String path) {
this.path = path;
}
public Object getBean(String beanid) throws Exception {
InputStream stream = this.getClass().getResourceAsStream(path);
Document document = getDoc(stream);
if (document == null) {
return null;
}
return getBean(beanid, document);
}
private Document getDoc(InputStream stream) {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(stream);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private Object getBean(String beanid, Document doc) throws Exception {
Element root = doc.getDocumentElement();
Element bean = getElement(root, "bean", "id", beanid);
Object ret = getBean(bean);
return ret;
}
private Object getBean(Element bean) throws Exception {
if (bean != null) {
String clazz = bean.getAttribute("class");
if (clazz != null) {
// 仅模拟简单情况,不支持构造器注入参数
Object obj = Class.forName(clazz).newInstance();
List<Element> properties = getElements(bean, "property");
for (Element e : properties) {
String name = e.getAttribute("name");
if (name == null) {
continue;
}
setValue(obj, name, e);
}
return obj;
}
}
return null;
}
private void setValue(Object obj, String name, Element element)
throws Exception {
String setMethod = getSetMethodName(name);
List<Element> valueList = getElements(element, "value");
// value
if (!valueList.isEmpty()) {
String value = valueList.get(0).getTextContent();
Field f = obj.getClass().getDeclaredField(name);
// 支持Integer类型和String类型
if (f.getType().getName().equals("int")) {
obj.getClass().getMethod(setMethod, int.class)
.invoke(obj, Integer.parseInt(value));
} else {
obj.getClass().getMethod(setMethod, String.class)
.invoke(obj, value);
}
} else {
List<Element> listList = getElements(element, "list");
// list
if (!listList.isEmpty()) {
List<Element> values = getElements(listList.get(0), "value");
List<String> list = new LinkedList<String>();
for (Element value : values) {
list.add(value.getTextContent());
}
obj.getClass().getMethod(setMethod, List.class)
.invoke(obj, list);
} else {
// ref
String beanid = getElements(element, "ref").get(0)
.getAttribute("bean");
Object refObj = getBean(beanid, element.getOwnerDocument());
Method[] methods = obj.getClass().getMethods();
for (Method method : methods) {
if (method.getName().equals(setMethod)) {
method.invoke(obj, refObj);
break;
}
}
}
}
}
private String getSetMethodName(String name) {
return "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
}
private Element getElement(Element root, String tagName, String attr,
String attrValue) {
NodeList list = root.getElementsByTagName(tagName);
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
String value = e.getAttribute(attr);
if (value != null && value.equals(attrValue)) {
return e;
}
}
}
return null;
}
private List<Element> getElements(Element root, String tagName) {
NodeList list = root.getChildNodes();
List<Element> elist = new LinkedList<Element>();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeName().equals(tagName)
&& node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
elist.add(e);
}
}
return elist;
}
}
接口 Person.java :
package com.darcy.note.spring;
public interface Person {
void speak();
}
服务实例 Chinese.java :
package com.darcy.note.spring;
import java.util.List;
import com.darcy.note.persistance.User;
import com.darcy.note.persistance.UserDAO;
public class Chinese implements Person {
private String name;
private int age;
private List<String> list;
private UserDAO dao;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void speak() {
System.out.println("a chinese name " + this.name + ", age " + this.age
+ ", list " + list.get(0));
User user = new User();
user.setName(name);
user.setPassword(String.valueOf(age));
user.setType(null);
dao.save(user);
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public UserDAO getDao() {
return dao;
}
public void setDao(UserDAO dao) {
this.dao = dao;
}
}
测试类:
package com.darcy.note.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) throws Exception {
// ApplicationContext context = new ClassPathXmlApplicationContext(
// "/services.xml");
BeanPool context = new BeanPool("/services.xml");
Person p = (Person) context.getBean("chinese");
p.speak();
// p = (Person) context.getBean("american");
// p.speak();
}
}