详解spring-仿springIOC功能的实现2

剩下的事情就是来扩充我们这个工具类,也就是实现其中的3个方法:
一、实现xml文件的解析:
   private void parseXml(String fileName) {
      try {
    SAXReader saxReader = new SAXReader();
    InputStream fs = this.getClass().getResourceAsStream   ("/"+ fileName);
    Document document = saxReader.read(fs);
Map<String, String> hsMap = new HashMap<String, String>();
hsMap.put("ns", "http://www.springframework.org/schema/beans");
XPath xpath = document.createXPath("ns:beans//ns:bean");
xpath.setNamespaceURIs(hsMap);
List beanList = xpath.selectNodes(document);
Iterator<Element> ite = beanList.iterator();
while (ite.hasNext()) {
  Element element = ite.next();
  Attribute attri1 = element.attribute("id");
  Attribute attri2 = element.attribute("class");
  BeanInformation bi = new BeanInformation();
  bi.setId(attri1.getValue());
  bi.setClassname(attri2.getValue());
  XPath xpath2 = element.createXPath("ns:property");
  xpath2.setNamespaceURIs(hsMap);
  List propertyList = xpath2.selectNodes(element);
  Iterator<Element> ite2 = propertyList.iterator();
  List<PropertyInfo> piList = new ArrayList<PropertyInfo>();
while (ite2.hasNext()) {
  Element ele = ite2.next();
  PropertyInfo pi = new PropertyInfo();
  pi.setName(ele.attributeValue("name"));
  pi.setRef(ele.attributeValue("ref"));
  pi.setValue(ele.attributeValue("value"));
  piList.add(pi);
}
  bi.setProList(piList);
  biList.add(bi);
}
} catch (Exception e) {
     e.printStackTrace();
}

}
另一种解析xml文件的方法:
  SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new FileInputStream(new File("").getAbsolutePath()+"\\src\\"+fileName));
List value = document.selectNodes("//article");
比较一下主要是两个调用selectNodes()方法时不一样,前者通过XPath类调用,后者通过Document类调用。前者用于解析包含类似于的头(也就是含有xml的命名别称):
    <beans xmlns="http://www.springframework.org/schema/beans">
而后者主要用于:
   <xml></xml>那样的文件。
实现dom4j解析xml文件主要搞懂以下几个就行:
xpath.selectNodes("你要解析的节点名称") 或者 document.selectNodes("
你要解析的节点名称");
Element类和Attribute类。
XPath xpath2 = element.createXPath("ns:property");
xpath2.setNamespaceURIs(hsMap);
第二种方式得到Element类:
Element firstElement = ite2.next().element("firstName");
 
二、实现实例化Bean:
private void instanceBean() {
   rator<BeanInformation> ite = biList.iterator();
while(ite.hasNext()) {
BeanInformation bi = ite.next();
if(bi.getClassname() != null && !"".equals(bi.getClassname())) {
try {
mapValue.put(bi.getId(), Class.forName(bi.getClassname()).newInstance());
}  catch (Exception e) {
e.printStackTrace();
}
}
}
}
  三、注入Bean:
  private void injectObject() {
for(BeanInformation beanInfo : biList) {
Object bean = mapValue.get(beanInfo.getId());
try {
PropertyDescriptor[] propertyDes = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyInfo proInfo : beanInfo.getProList()) {
for(PropertyDescriptor proDes : propertyDes) {
if(proInfo.getName().equals(proDes.getName())) {
Method setter = proDes.getWriteMethod();
Object value = null;
if(proInfo.getRef()!=null&&!proInfo.getRef().equals("")){
value = mapValue.get(proInfo.getRef());
}else {
value =  ConvertUtils.convert(proInfo.getValue(), proDes.getPropertyType());
}

setter.invoke(bean, value);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
 

你可能感兴趣的:(spring,xml,bean)