解析spring加载xml与管理bean的实现
大家开发的时候都知道,spring框架。然后我们大多数是知其然不知其所以然。
今天我们一起,来看看,spring是如何读取xml文件,管理bean的。
首选我们,需要spring.jar和Dom4j.jar两个包。
开发部署:
一。
搭建一个java项目:

二。编写一个spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="myService" class="com.demo.spring.MyServiceImpl"/> </beans>
里面是我们需要加载管理的bean
三。
创建接口类Myservice和接口实现类。
package com.demo.spring; public interface MyService { void save(); } package com.demo.spring; public class MyServiceImpl implements MyService { @Override public void save() { System.out.println("spring解析成功!"); } }
四。我们需要自己创建一个解析spring.xml的类。并且来做我们的applicationContext容器来管理 xml中的bean。
那么我们首选创建一个实体类,用来接收我们解析xml出来的值。
package com.demo.spring; public class MyBeans { private String id; private String className; public MyBeans(String id, String className) { this.id = id; this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
创建解析xml的类MyClassPathXmlApplicationContext
package com.demo.spring; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; /** * 相当于spring的,解析类。 * @author huojg21442 * */ public class MyClassPathXmlApplicationContext { //装载实例的化bean private Map<String,Object> beanMap=new HashMap<String,Object>(); //装置配置信息文件的属性和值 private List<MyBeans> beanList =new ArrayList<MyBeans>(); public MyClassPathXmlApplicationContext(String filename) { //第一步解析spring.xml文件 readXml(filename); //第二步通过反射,实例化所有注入bean initBeans(); } /** * 通过反射机制,初始化配置文件中的bean */ public void initBeans(){ for(MyBeans bean:beanList){ try { String ss=bean.getClassName(); if(bean.getClassName()!=null&&!"".equals(bean.getClassName())){ beanMap.put(bean.getId(),Class.forName(bean.getClassName()).newInstance()); } } catch (Exception e) { e.printStackTrace(); } } } /** * 解析配置文件,把解析后的bean设置到实体中,并保持到list * * @param filename */ public void readXml(String filename){ SAXReader reader=new SAXReader(); Document doc; URL xmlPath=this.getClass().getClassLoader().getResource(filename); try { Map<String, String> nsMap = new HashMap<String, String>(); doc = reader.read(xmlPath); // 取得文档下所有节点 Element employees = doc.getRootElement(); for(Iterator i=employees.elementIterator();i.hasNext();){ Element employee=(Element)i.next(); String id = employee.attributeValue("id"); String cn = employee.attributeValue("class"); //自定义实体bean,保存配置文件中id和class MyBeans beans = new MyBeans(id, cn); beanList.add(beans); } } catch (Exception e) { e.printStackTrace(); } } public Object getBean(String beanId) { return beanMap.get(beanId); } }
最后创建测试类:
package com.demo.spring; /** * * @author huojg21442 * * 解刨spring管理bean以及读取文件的源代码编写解析。 * */ public class TestSpring { public static void main(String[] args) { MyClassPathXmlApplicationContext ctx = new MyClassPathXmlApplicationContext("spring.xml"); MyService myService = (MyService) ctx.getBean("myService"); myService.save(); } }
结果输出:
spring解析成功!
其中最重要的是,解析xml的过程。我们利用map存起来封装在实体类中。然后利用反射的创建对象出来。
                    
                
                
            
        
浙公网安备 33010602011771号