[Java] 模拟简单的SpringIOC →→→→→进入此内容的聊天室

来自 , 2019-06-16, 写在 Java, 查看 162 次.
URL http://www.code666.cn/view/6395ebd0
  1. public class SpringIOC {
  2.         private String XMLPath;
  3.  
  4.         /**
  5.          * @param XMLPath
  6.          *            配置文件的路径
  7.          */
  8.         public SpringIOC(String XMLPath) {
  9.                 this.XMLPath = XMLPath;
  10.         }
  11.  
  12.         public Object getBean(String beanId) throws Throwable {
  13.                 // 1.解析配置文件
  14.                 Document doc = this.loadXML();
  15.                 // 2.根据beanId定位到<bean>标签
  16.                 Element beanElement = this.getBeanElement(doc, beanId);
  17.                 // 3.获取beanElement的class属性的值
  18.                 String classPath = this.getClassPath(beanElement);
  19.                 // 4.通过class属性值利用反射技术构造bean实例
  20.                 Object obj = this.createObject(classPath);
  21.                 // 5.使用beanUtils给对象设置
  22.                 obj = this.setObeject(beanElement, obj);
  23.  
  24.                 return obj;
  25.         }
  26.  
  27.         /**
  28.          * 加载xml配置文件
  29.          *
  30.          * @return
  31.          * @throws Throwable
  32.          */
  33.         private Document loadXML() throws Throwable {
  34.                 return new SAXReader().read(new File(XMLPath));
  35.         }
  36.  
  37.         /**
  38.          * 根据beanId定位到对应的bean标签
  39.          *
  40.          * @param doc
  41.          * @param beanId
  42.          * @return Element
  43.          */
  44.         private Element getBeanElement(Document doc, String beanId) {
  45.                 return (Element) doc.selectSingleNode("//bean[@id='" + beanId + "']");
  46.         }
  47.  
  48.         /**
  49.          * 得到beanElement的class属性值
  50.          *
  51.          * @param beanElement
  52.          * @return
  53.          */
  54.         private String getClassPath(Element beanElement) {
  55.                 return beanElement.attributeValue("class");
  56.         }
  57.  
  58.         /**
  59.          * 通过class属性值利用反射技术构造bean实例
  60.          *
  61.          * @param classPath
  62.          * @return
  63.          */
  64.         private Object createObject(String classPath) throws Throwable {
  65.                 return Class.forName(classPath).newInstance();
  66.         }
  67.  
  68.         /**
  69.          * 使用工具类beanUtils给对象设值
  70.          *
  71.          * @param obj
  72.          * @return
  73.          * @throws Throwable
  74.          */
  75.         @SuppressWarnings("unchecked")
  76.         private Object setObeject(Element beanElement, Object obj) throws Throwable {
  77.                 List<Element> list = beanElement.elements();
  78.                 for (Element element : list) {
  79.                         String name = element.attributeValue("name");
  80.                         String value = element.attributeValue("value");
  81.                         BeanUtils.setProperty(obj, name, value);
  82.                 }
  83.                 return obj;
  84.         }
  85. }

回复 "模拟简单的SpringIOC"

这儿你可以回复上面这条便签

captcha