简易的通过xml实现自动实例化bean

  • 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);
	}
}	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值