2、xml配置文件: 3、實例對象: 4、BeanFactory工廠: ...
1、通過bean工廠實現讀取xml文件,並實例化對象,實現自動註入。
package com.pri.test; import com.pri.factory.BeanFactory; import com.pri.service.UserService; import org.junit.Test; public class BeanFactoryTest { @Test public void test(){ BeanFactory beanFactory = BeanFactory.getBeanFactory(); UserService user = (UserService)beanFactory.getBean("user"); user.save(); } }
2、xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans > <bean id="user" class="com.pri.service.impl.UserServiceImpl01"> <property name="name" value="李四"/> </bean> </beans>
3、實例對象:
package com.pri.service.impl; import com.pri.service.UserService; public class UserServiceImpl01 implements UserService{ private String name; public String getName() { return name;} public void setName(String name) { this.name = name;} @Override public void save(){ System.out.println("UserService被調用........::"+name); } }
4、BeanFactory工廠:
package com.pri.factory; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class BeanFactory { /** * 私有化構造,禁止以new的方式獲取BeanFactory實例 */ private BeanFactory(){} /** * 靜態內部類,保證只有一個BeanFactory */ static class SingleBeanFactory{ static BeanFactory BEANFACTORY = new BeanFactory(); } /** * 對外提供一個獲取BeanFactory方法 * @return */ public static BeanFactory getBeanFactory(){ return SingleBeanFactory.BEANFACTORY; } public Object getBean(String id) { try { Element element = getElement(id); String classPath = element.attribute("class").getValue(); //根據類的全限定名創建Class對象 Class<?> clazz = Class.forName(classPath); //創建實例 Object obj = clazz.newInstance(); //為bean對象賦值 setAttributeValue(clazz,element,obj); return obj; } catch (Exception e) { e.printStackTrace(); return null; } } private Element getElement(String id) throws DocumentException { SAXReader saxReader = new SAXReader(); Document document = saxReader.read("src/myconfig.xml"); //精確定位到id屬性值為傳入字元串的bean元素 return (Element) document.selectSingleNode("//bean[@id='" + id + "']"); } private void setAttributeValue(Class<?> clazz, Element element,Object obj) throws IntrospectionException, IllegalAccessException, InvocationTargetException { Element subElement = element.element("property"); //使用set方法賦值 if (subElement != null) { String name = subElement.attributeValue("name"); String valueStr = subElement.attributeValue("value"); //創建屬性描述器 PropertyDescriptor descriptor = new PropertyDescriptor(name, clazz); //獲取該屬性的修飾符 String s = descriptor.getPropertyType().getName(); //獲得寫方法 Method writeMethod = descriptor.getWriteMethod(); //調用寫方法 Object value = null; if (s.equalsIgnoreCase("int")){ value = Integer.parseInt(valueStr); }else if (s.equalsIgnoreCase("long")){ value = Long.parseLong(valueStr); }else if (s.equalsIgnoreCase("short")){ value = Short.parseShort(valueStr); }else if (s.equalsIgnoreCase("double")){ value = Double.parseDouble(valueStr); }else if (s.equalsIgnoreCase("float")){ value = Float.parseFloat(valueStr); }else if (s.equalsIgnoreCase("byte")) { value = Byte.valueOf(valueStr); }else{ value = valueStr; } writeMethod.invoke(obj, value); } } }