标签:链接 factor 初始化 resource 简单的 use nbsp pat cas
这里我们简单的实现一下spring中的初始化bean,以大概了解他的流程。受委屈几乎是一个人成长最快的途径,吃下去的是委屈,消化掉后得到的是格局。
测试的项目结构如下:
package com.spring1.bean; public class FirstBean { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUserInfo() { return username + ", " + password; } }
package com.spring1.bean; public class SecondBean { private String name; private int age; 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 String getUserInfo() { return name + ", " + age; } }
package com.spring1.factory; import java.util.HashMap; import java.util.Map; public class BeanFactory { Map<String, Object> map = new HashMap<>(); public Object getBean(String key) { return map.get(key); } public <T> T getBean(String key, Class<T> tClass) { return (T) map.get(key); } public void setBean(String name, Object object) { map.put(name, object); } }
package com.spring1.parse; import com.spring1.SpringUtil; import com.spring1.factory.BeanFactory; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.InputStream; import java.lang.reflect.Method; import java.util.List; public class ParseXmlBean { public static BeanFactory parseXmlBean(String filepath) { BeanFactory beanFactory = new BeanFactory(); SAXReader reader = new SAXReader(); try { InputStream stream = ParseXmlBean.class.getClassLoader().getResourceAsStream(filepath); Document document = reader.read(stream); Element rootElement = document.getRootElement(); List<Element> entries = rootElement.elements("bean"); for (Element entry : entries) { String beanId = entry.attributeValue("id"); String beanClass = entry.attributeValue("class"); Class<?> bean = Class.forName(beanClass); Object beanObject = bean.newInstance(); for (Object subEntry : entry.elements("property")) { String name = ((Element) subEntry).attributeValue("name"); String value = ((Element) subEntry).attributeValue("value"); String dataType = ((Element) subEntry).attributeValue("dataType"); Method method; if (dataType == null) { method = bean.getMethod(getMethodName(name), new Class[]{String.class}); method.invoke(beanObject, new Object[]{value}); } else if (dataType.equals("int")) { method = bean.getMethod(getMethodName(name), new Class[]{int.class}); method.invoke(beanObject, new Object[]{Integer.valueOf(value)}); } } beanFactory.setBean(beanId, beanObject); } } catch (Exception e) { e.printStackTrace(); } return beanFactory; } private static String getMethodName(String name) { return "set" + SpringUtil.firstLetterUpper(name); } }
package com.spring1; public class SpringUtil { public static String firstLetterUpper(String name) { return name.substring(0, 1).toUpperCase() + name.substring(1); } }
package com.spring1.main; import com.spring1.bean.FirstBean; import com.spring1.bean.SecondBean; import com.spring1.factory.BeanFactory; import com.spring1.parse.ParseXmlBean; import org.junit.Test; public class MainTest { @Test public void test_1() { BeanFactory beanFactory = ParseXmlBean.parseXmlBean("config/bean.xml"); FirstBean firstBean = beanFactory.getBean("firstBean", FirstBean.class); String userInfo = firstBean.getUserInfo(); System.out.println(userInfo); SecondBean secondBean = (SecondBean) beanFactory.getBean("secondBean"); String userInfo1 = secondBean.getUserInfo(); System.out.println(userInfo1); } }
运行的结果如下:
huhx, 1234 liuling, 21
<beans> <bean id="firstBean" class="com.spring1.bean.FirstBean"> <property name="username" value="huhx"/> <property name="password" value="1234"/> </bean> <bean id="secondBean" class="com.spring1.bean.SecondBean"> <property name="age" value="21" dataType="int"/> <property name="name" value="liuling"/> </bean> </beans>
标签:链接 factor 初始化 resource 简单的 use nbsp pat cas
原文地址:http://www.cnblogs.com/huhx/p/baseusemyspring1.html