自定义框架技术基础之一——自己实现一个Spring的IOC(控制反转)BeanFactory
一、
实体类 二、 Src/com/jisong/ioc/Zjsioc.Xml <?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="student" class="com.jisong.ioc.entity.Student"> <property name="no" type="java.lang.String"> <value>1</value> </property> <property name="name" type="java.lang.String"> <value> </property> </bean> <bean id="student2" class="com.jisong.ioc.entity.Student"> <property name="no" type="java.lang.String"> <value>44</value> </property> <property name="name" type="java.lang.String"> <value> </property> </bean> </beans> 三、 package com.jisong.ioc; import java.beans.PropertyDescriptor; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.jisong.ioc.entity.Student; /** * Bean * @author */ public class BeanFactoryByReflect { /** * beanMap, */ private Map<String, Object> beanMap = new HashMap<String, Object>(); /** * * @param xmlUri */ public void init(String xmlUri) throws Exception{ try { //1. //1.1 SAXReader reader = new SAXReader();// // ClassLoader classLoader = Thread.currentThread() // .getContextClassLoader();// // InputStream in = classLoader.getResourceAsStream(xml);// InputStream in=this.getClass().getResourceAsStream(xmlUri);// //1.2. Document doc = reader.read(in);// //1.3. Element root = doc.getRootElement();// Element elBean = null;// // 2. for (Iterator iteBean = root.elementIterator("bean"); iteBean .hasNext();) { elBean = (Element) iteBean.next();// // 2.1 Attribute atrId = elBean.attribute("id"); Attribute atrClass = elBean.attribute("class"); // 2.2 Class clsBean = Class.forName(atrClass.getText()); // 2.3 // Field pds[] = clsBean.getDeclaredFields(); // Map<String, Field> mapProp = new HashMap<String, Field>(); for (Field pd : pds) { mapProp.put(pd.getName(), pd); } // 2.4 Object obj = clsBean.newInstance(); Method mSet = null;// // 2.5 for (Iterator iteProp = elBean.elementIterator("property"); iteProp .hasNext();) { // 2.5.1 Element elProp = (Element) iteProp.next(); // 2.5.2 Attribute atrName = elProp.attribute("name"); Attribute atrType= elProp.attribute("type"); Class clsType=Class.forName(atrType.getText()); // 2.5.3 String strValue = null; // for (Iterator iteValue = elProp.elementIterator("value"); iteValue .hasNext();) { Element elValue = (Element) iteValue.next(); strValue = elValue.getText(); break; } // 2.5.4 Field tmpPd=mapProp.get(atrName.getValue()); if(tmpPd!=null){ String fieldName=tmpPd.getName(); String methodName="set"+ fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); mSet=clsBean.getMethod(methodName, clsType);// mSet.invoke(obj, strValue);// } } // beanMap.put(atrId.getText(), obj); } } catch (Exception e) { throw e; } } /** * * * @param beanId * xml * @return */ public Object getBean(String beanId) { Object obj = beanMap.get(beanId); return obj; } /** * */ public static void main(String[] args) { try { BeanFactoryByReflect factory = new BeanFactoryByReflect(); factory.init("/com/jisong/ioc/zjsioc.xml"); Student stud = (Student) factory.getBean("student"); System.out.println("mso-tab-count: 3"> System.out.println("mso-tab-count: 3"> System.out.println("mso-tab-count: 3"> stud=(Student)factory.getBean("student2"); |
自定义框架技术基础之一——自己实现一个Spring的IOC(控制反转)BeanFactory
一、
实体类 二、 Src/com/jisong/ioc/Zjsioc.Xml <?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="student" class="com.jisong.ioc.entity.Student"> <property name="no" type="java.lang.String"> <value>1</value> </property> <property name="name" type="java.lang.String"> <value> </property> </bean> <bean id="student2" class="com.jisong.ioc.entity.Student"> <property name="no" type="java.lang.String"> <value>44</value> </property> <property name="name" type="java.lang.String"> <value> </property> </bean> </beans> 三、 package com.jisong.ioc; import java.beans.PropertyDescriptor; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.jisong.ioc.entity.Student; /** * Bean * @author */ public class BeanFactoryByReflect { /** * beanMap, */ private Map<String, Object> beanMap = new HashMap<String, Object>(); /** * * @param xmlUri */ public void init(String xmlUri) throws Exception{ try { //1. //1.1 SAXReader reader = new SAXReader();// // ClassLoader classLoader = Thread.currentThread() // .getContextClassLoader();// // InputStream in = classLoader.getResourceAsStream(xml);// InputStream in=this.getClass().getResourceAsStream(xmlUri);// //1.2. Document doc = reader.read(in);// //1.3. Element root = doc.getRootElement();// Element elBean = null;// // 2. for (Iterator iteBean = root.elementIterator("bean"); iteBean .hasNext();) { elBean = (Element) iteBean.next();// // 2.1 Attribute atrId = elBean.attribute("id"); Attribute atrClass = elBean.attribute("class"); // 2.2 Class clsBean = Class.forName(atrClass.getText()); // 2.3 // Field pds[] = clsBean.getDeclaredFields(); // Map<String, Field> mapProp = new HashMap<String, Field>(); for (Field pd : pds) { mapProp.put(pd.getName(), pd); } // 2.4 Object obj = clsBean.newInstance(); Method mSet = null;// // 2.5 for (Iterator iteProp = elBean.elementIterator("property"); iteProp .hasNext();) { // 2.5.1 Element elProp = (Element) iteProp.next(); // 2.5.2 Attribute atrName = elProp.attribute("name"); Attribute atrType= elProp.attribute("type"); Class clsType=Class.forName(atrType.getText()); // 2.5.3 String strValue = null; // for (Iterator iteValue = elProp.elementIterator("value"); iteValue .hasNext();) { Element elValue = (Element) iteValue.next(); strValue = elValue.getText(); break; } // 2.5.4 Field tmpPd=mapProp.get(atrName.getValue()); if(tmpPd!=null){ String fieldName=tmpPd.getName(); String methodName="set"+ fieldName.substring(0,1).toUpperCase()+fieldName.substring(1); mSet=clsBean.getMethod(methodName, clsType);// mSet.invoke(obj, strValue);// } } // beanMap.put(atrId.getText(), obj); } } catch (Exception e) { throw e; } } /** * * * @param beanId * xml * @return */ public Object getBean(String beanId) { Object obj = beanMap.get(beanId); return obj; } /** * */ public static void main(String[] args) { try { BeanFactoryByReflect factory = new BeanFactoryByReflect(); factory.init("/com/jisong/ioc/zjsioc.xml"); Student stud = (Student) factory.getBean("student"); System.out.println("mso-tab-count: 3"> System.out.println("mso-tab-count: 3"> System.out.println("mso-tab-count: 3"> stud=(Student)factory.getBean("student2"); |