http://zhangjunhd.blog.51cto.com/113473/126545java
<?xml version=
"1.0" encoding=
"UTF-8"?>
<beans>
<bean id=
"me" class=
"com.zj.ioc.di.imp.Person">
<property name=
"name">
<value>ZJ</value>
</property>
<property name=
"age">
<value>26</value>
</property>
<property name=
"height">
<value>1.78</value>
</property>
</bean>
<bean id=
"you" class=
"com.zj.ioc.di.imp.Person">
<property name=
"name">
<value>Mary</value>
</property>
<property name=
"age">
<value>27</value>
</property>
<property name=
"height">
<value>1.66</value>
</property>
</bean>
<bean id=
"myList" class=
"com.zj.ioc.di.imp.ListOne">
<property name=
"msg">
<list>
<value>java</value>
<value>c</value>
<value>windows</value>
</list>
</property>
</bean>
<bean id=
"mySet" class=
"com.zj.ioc.di.imp.SetOne">
<property name=
"msg">
<set>
<value>tom</value>
<value>cat</value>
<value>dog</value>
</set>
</property>
</bean>
<bean id=
"myMap" class=
"com.zj.ioc.di.imp.MapOne">
<property name=
"msg">
<map>
<entry key=
"c">
<value>CHINA</value>
</entry>
<entry key=
"j">
<value>JAPAN</value>
</entry>
<entry key=
"k">
<value>KOREA</value>
</entry>
</map>
</property>
</bean>
<bean id=
"us" class=
"com.zj.ioc.di.imp.Persons">
<property name=
"i">
<ref bean=
"me" />
</property>
<property name=
"u">
<ref bean=
"you" />
</property>
</bean>
</beans>
|
package com.zj.ioc.di.imp;
public
class Person {
private String name;
private
int age;
private
float height;
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
float getHeight() {
return height;}
public
void setHeight(
float height) {
this.height = height;}
}
|
package com.zj.ioc.di.imp;
import java.util.List;
public
class ListOne {
private List<String> msg;
public List<String> getMsg() {
return msg;}
public
void setMsg(List<String> msg) {
this.msg = msg;}
}
|
package com.zj.ioc.di.imp;
import java.util.Set;
public
class SetOne {
private Set<String> msg;
public Set<String> getMsg() {
return msg;}
public
void setMsg(Set<String> msg) {
this.msg = msg;}
}
|
package com.zj.ioc.di.imp;
import java.util.Map;
public
class MapOne {
private Map<String,String> msg;
public Map<String, String> getMsg() {
return msg;}
public
void setMsg(Map<String, String> msg) {
this.msg = msg;}
}
|
package com.zj.ioc.di.imp;
public
class Persons {
private Person i;
private Person u;
public Person getI() {
return i;}
public
void setI(Person i) {
this.i = i;}
public Person getU() {
return u;}
public
void setU(Person u) {
this.u = u;}
}
|
private Map<String, Object> beanMap =
new HashMap<String, Object>();
……
public Object getBean(String beanId) {
Object obj = beanMap.get(beanId);
return obj;
}
|
BeanFactory factory =
new BeanFactory();
factory.init("setting.xml");
Person p1 = (Person) factory.getBean("me");
|
public
void init(String xmlUri)
throws Exception {
SAXReader saxReader =
new SAXReader();
File file =
new File(xmlUri);
try {
saxReader.addHandler("/beans/bean",
new BeanHandler());
saxReader.read(file);
}
catch (DocumentException e) {
System.
out.println(e.getMessage());
}
}
|
private
class BeanHandler
implements ElementHandler {
Object obj =
null;
public
void .Start(ElementPath path) {
Element beanElement = path.getCurrent();
Attribute classAttribute = beanElement.attribute("class");
Class<?> bean =
null;
try {
bean = Class.
forName(classAttribute.getText());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
Field fields[] = bean.getDeclaredFields();
Map<String, Field> mapField =
new HashMap<String, Field>();
for (Field field : fields)
mapField.put(field.getName(), field);
try {
obj = bean.newInstance();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
path.addHandler("property",
new PropertyHandler(mapField, obj));
}
public
void .End(ElementPath path) {
Element beanElement = path.getCurrent();
Attribute idAttribute = beanElement.attribute("id");
beanMap.put(idAttribute.getText(), obj);
path.removeHandler("property");
}
}
|
private
class PropertyHandler
implements ElementHandler {
Map<String, Field> mapField;
Object obj;
public PropertyHandler(Map<String, Field> mapField, Object obj) {
this.mapField = mapField;
this.obj = obj;
}
public
void .Start(ElementPath path) {
Element propertyElement = path.getCurrent();
Attribute nameAttribute = propertyElement.attribute("name");
path.addHandler("value",
new ValueHandler(mapField, obj,
nameAttribute));
path.addHandler("list",
new ListHandler(mapField, obj,
nameAttribute));
path.addHandler("set",
new SetHandler(mapField, obj,
nameAttribute));
path.addHandler("map",
new MapHandler(mapField, obj,
nameAttribute));
path.addHandler("ref",
new RefHandler(mapField, obj,
nameAttribute));
}
public
void .End(ElementPath path) {
path.removeHandler("value");
path.removeHandler("list");
path.removeHandler("set");
path.removeHandler("map");
path.removeHandler("ref");
}
}
|
private
void setFieldValue(Object obj, Field field, String value) {
String fieldType = field.getType().getSimpleName();
try {
if (fieldType.equals("int"))
field.setInt(obj,
new Integer(value));
else
if (fieldType.equals("float"))
field.setFloat(obj,
new Float(value));
else
if (fieldType.equals("boolean"))
field.setBoolean(obj,
new Boolean(value));
else
if (fieldType.equals("char"))
field.setChar(obj, value.charAt(0));
else
if (fieldType.equals("double"))
field.setDouble(obj,
new Double(value));
else
if (fieldType.equals("long"))
field.setLong(obj,
new Long(value));
else
field.set(obj, value);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private
void setFieldValue(Object obj, Field field, List<String> value) {
try {
field.set(obj, value);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
|
public
static
void main(String[] args) {
try {
BeanFactory factory =
new BeanFactory();
factory.init("setting.xml");
Person p1 = (Person) factory.getBean("me");
System.
out.print(p1.getName() + " ");
System.
out.print(p1.getAge() + " ");
System.
out.println(p1.getHeight());
Person p2 = (Person) factory.getBean("you");
System.
out.print(p2.getName() + " ");
System.
out.print(p2.getAge() + " ");
System.
out.println(p2.getHeight());
ListOne list = (ListOne) factory.getBean("myList");
System.
out.println(list.getMsg());
SetOne set = (SetOne) factory.getBean("mySet");
System.
out.println(set.getMsg());
MapOne map = (MapOne) factory.getBean("myMap");
System.
out.println(map.getMsg());
Persons us = (Persons) factory.getBean("us");
System.
out.println(us.getI());
System.
out.println(us.getU());
}
catch (Exception e) {
e.printStackTrace();
}
}
|