📄 beanfactory.java
字号:
package com.zj.ioc.di.imp;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.SAXReader;
public class BeanFactory {
private Map<String, Object> beanMap = new HashMap<String, Object>();
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());
}
}
public Object getBean(String beanId) {
Object obj = beanMap.get(beanId);
return obj;
}
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();
}
}
private void setFieldValue(Object obj, Field field, Set<String> value) {
try {
field.set(obj, value);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private void setFieldValue(Object obj, Field field,
Map<String, String> value) {
try {
field.set(obj, value);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private void setFieldValue(Object obj, Field field, Object bean) {
try {
field.set(obj, bean);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private class BeanHandler implements ElementHandler {
Object obj = null;
public void onStart(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 onEnd(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 onStart(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 onEnd(ElementPath path) {
path.removeHandler("value");
path.removeHandler("list");
path.removeHandler("set");
path.removeHandler("map");
path.removeHandler("ref");
}
}
private class ValueHandler implements ElementHandler {
Map<String, Field> mapField;
Object obj;
Attribute nameAttribute;
public ValueHandler(Map<String, Field> mapField, Object obj,
Attribute nameAttribute) {
this.mapField = mapField;
this.obj = obj;
this.nameAttribute = nameAttribute;
}
public void onStart(ElementPath path) {
}
public void onEnd(ElementPath path) {
Element valueElement = path.getCurrent();
String strValue = null;
strValue = valueElement.getText();
Field tmpField = mapField.get(nameAttribute.getValue());
if (tmpField != null) {
tmpField.setAccessible(true);
setFieldValue(obj, tmpField, strValue);
}
}
}
private class ListHandler implements ElementHandler {
Map<String, Field> mapField;
Object obj;
Attribute nameAttribute;
List<String> list;
public ListHandler(Map<String, Field> mapField, Object obj,
Attribute nameAttribute) {
this.mapField = mapField;
this.obj = obj;
this.nameAttribute = nameAttribute;
}
public void onStart(ElementPath path) {
list = new ArrayList<String>();
path.addHandler("value", new ListValueHandler(list));
}
public void onEnd(ElementPath path) {
path.removeHandler("value");
Field tmpField = mapField.get(nameAttribute.getValue());
if (tmpField != null) {
tmpField.setAccessible(true);
setFieldValue(obj, tmpField, list);
}
}
}
private class ListValueHandler implements ElementHandler {
List<String> list;
public ListValueHandler(List<String> list) {
this.list = list;
}
public void onStart(ElementPath path) {
}
public void onEnd(ElementPath path) {
Element valueElement = path.getCurrent();
String strValue = null;
strValue = valueElement.getText();
list.add(strValue);
}
}
private class SetHandler implements ElementHandler {
Map<String, Field> mapField;
Object obj;
Attribute nameAttribute;
Set<String> set;
public SetHandler(Map<String, Field> mapField, Object obj,
Attribute nameAttribute) {
this.mapField = mapField;
this.obj = obj;
this.nameAttribute = nameAttribute;
}
public void onStart(ElementPath path) {
set = new HashSet<String>();
path.addHandler("value", new SetValueHandler(set));
}
public void onEnd(ElementPath path) {
path.removeHandler("value");
Field tmpField = mapField.get(nameAttribute.getValue());
if (tmpField != null) {
tmpField.setAccessible(true);
setFieldValue(obj, tmpField, set);
}
}
}
private class SetValueHandler implements ElementHandler {
Set<String> set;
public SetValueHandler(Set<String> set) {
this.set = set;
}
public void onStart(ElementPath path) {
}
public void onEnd(ElementPath path) {
Element valueElement = path.getCurrent();
String strValue = null;
strValue = valueElement.getText();
set.add(strValue);
}
}
private class MapHandler implements ElementHandler {
Map<String, Field> mapField;
Object obj;
Attribute nameAttribute;
Map<String, String> map;
public MapHandler(Map<String, Field> mapField, Object obj,
Attribute nameAttribute) {
this.mapField = mapField;
this.obj = obj;
this.nameAttribute = nameAttribute;
}
public void onStart(ElementPath path) {
map = new HashMap<String, String>();
path.addHandler("entry", new MapEntryHandler(map));
}
public void onEnd(ElementPath path) {
path.removeHandler("entry");
Field tmpField = mapField.get(nameAttribute.getValue());
if (tmpField != null) {
tmpField.setAccessible(true);
setFieldValue(obj, tmpField, map);
}
}
}
private class MapEntryHandler implements ElementHandler {
Map<String, String> map;
public MapEntryHandler(Map<String, String> map) {
this.map = map;
}
public void onStart(ElementPath path) {
Element entryElement = path.getCurrent();
Attribute keyAttribute = entryElement.attribute("key");
path.addHandler("value", new MapValueHandler(keyAttribute, map));
}
public void onEnd(ElementPath path) {
path.removeHandler("value");
}
}
private class MapValueHandler implements ElementHandler {
Map<String, String> map;
Attribute keyAttribute;
public MapValueHandler(Attribute keyAttribute, Map<String, String> map) {
this.keyAttribute = keyAttribute;
this.map = map;
}
public void onStart(ElementPath path) {
}
public void onEnd(ElementPath path) {
Element valueElement = path.getCurrent();
String strValue = null;
strValue = valueElement.getText();
map.put(keyAttribute.getText(), strValue);
}
}
private class RefHandler implements ElementHandler {
Map<String, Field> mapField;
Object obj;
Attribute nameAttribute;
Object bean;
public RefHandler(Map<String, Field> mapField, Object obj,
Attribute nameAttribute) {
this.mapField = mapField;
this.obj = obj;
this.nameAttribute = nameAttribute;
}
public void onStart(ElementPath path) {
Element refElement = path.getCurrent();
Attribute beanAttribute = refElement.attribute("bean");
bean = beanMap.get(beanAttribute.getText());
}
public void onEnd(ElementPath path) {
Field tmpField = mapField.get(nameAttribute.getValue());
if (tmpField != null) {
tmpField.setAccessible(true);
setFieldValue(obj, tmpField, bean);
}
}
}
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -