- bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="obj" class="java.lang.Object" flag="true"></bean>
<bean id="user" class="com.qhf.pojo.User" flag="true"></bean>
</beans>
- Bean 类来封装bean.xml 的每个bean 节点
public class Bean {
private String id;
private String clazz;
private boolean flag;
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 boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public String toString() {
return "Bean [id=" + id + ", clazz=" + clazz + ", flag=" + flag + "]";
}
}
- 实现xml 读取数据并封装到 Bean 类中,使用流,java 的document 类
public abstract class XmlToBean {
protected Map<String, Bean> beanMap = new ConcurrentHashMap<String, Bean>();
public XmlToBean(String xml) {
InputStream is = ClassLoader.getSystemResourceAsStream(xml);
handleSteam(is);
}
protected void handleSteam(InputStream is) {
try {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
handleDoc(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
private void handleDoc(Document doc) {
NodeList beans = doc.getElementsByTagName("bean");
for(int i = 0; i < beans.getLength(); i++) {
Node bean = beans.item(i);
handlebean(bean);
}
}
private void handlebean(Node bean) {
NamedNodeMap attributes = bean.getAttributes();
String id = attributes.getNamedItem("id").getNodeValue();
String clazz = attributes.getNamedItem("class").getNodeValue();
String flag = attributes.getNamedItem("flag").getNodeValue();
Bean b = new Bean();
b.setId(id);
b.setClazz(clazz);
b.setFlag(new Boolean(flag));
storeBean(b);
}
private void storeBean(Bean b) {
beanMap.put(b.getId(), b);
}
}
- BeanFactory 接口
public interface BeanFactory {
public <T>T getBean(String key, Class<T> cls);
public <T>T getBean(Class<T> cls);
}
- 工厂类
public class ClassPathXmlApplicationContext extends XmlToBean implements BeanFactory{
private Map<String, Object> objMap = new ConcurrentHashMap<String, Object>();
public ClassPathXmlApplicationContext(String xml) {
super(xml);
}
@Override
public <T>T getBean(String key, Class<T> cls) {
if(!beanMap.containsKey(key)) {
System.out.println("no such key");
return null;
}
String name = beanMap.get(key).getClazz();
if(!name.equals(cls.getName())) {
System.out.println("no such bean");
return null;
}
if(objMap.get(key) == null) {
Object obj = InstanceUtil.newInstance(cls);
objMap.put(key, obj);
return (T)obj;
}
return (T)objMap.get(key);
}
@Override
public <T> T getBean(Class<T> cls) {
return null;
}
}
- 反射实现new 新对象
public class InstanceUtil {
public static Object newInstance(Class<?> cls) {
Object instance = null;
try {
Constructor<?> constructor = cls.getDeclaredConstructor();
if(!constructor.isAccessible())
constructor.setAccessible(true);
instance = constructor.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return instance;
}
}
- 测试类
public class TestXmlBeanFactory {
public static void main(String[] args) {
ClassPathXmlApplicationContext xbf = new ClassPathXmlApplicationContext("bean.xml");
Object obj1 = xbf.getBean("obj", Object.class);
Object obj2 = xbf.getBean("obj", Object.class);
System.out.println(obj1 == obj2);
User user = xbf.getBean("user", User.class);
System.out.println(user);
}
}