笨鸟之Spring实例化bean的模拟实现

来源:互联网 发布:网络攻防技术包含哪些 编辑:程序博客网 时间:2024/06/06 04:16

我们平时都说bean交由spring来管理,那spring容器到底怎么样读取和实例化bean的呢?

现在我们就来简单的模拟下spring实例化过程:

1,建立自己的配置文件applicationContext.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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd           http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd           http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">                      <bean id="ScoreRule" class="cn.zhou.ScoreRule" lazy-init="false"             init-method="init" destroy-method="destory"></bean></beans>           

2,建立自己的context类ZhouClassPathXmlApplicationContext:

package cn.zhou.spring;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;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;import cn.zhou.ScoreRule;/** * 模拟spring实例化bean * @throws Exception  */public class ZhouClassPathXmlApplicationContext {private String fileName;private List<ScoreRule> scoreRuleList = new ArrayList<ScoreRule>(); //存放Reader读取的beanprivate Map<String, Object> beans = new HashMap<String, Object>(); //接收初始化的bean//提供获取bean的方法public Object getBean(String beanName) {return beans.get(beanName);}//构造函数public ZhouClassPathXmlApplicationContext(String fileName) throws Exception {this.readXml(fileName);//读取this.instance();//初始化//this.injectObjec();//注入bean}private void injectObjec() {//注入bean}private void instance() throws Exception {for (ScoreRule scoreRule : scoreRuleList) {Object clazz = Class.forName(scoreRule.getClassName()).newInstance();beans.put(scoreRule.getId(), clazz);}}//read applicationContext.xmlprivate void readXml(String fileName) throws Exception {SAXReader reader = new SAXReader();URL path = this.getClass().getClassLoader().getResource(fileName);Document doc = reader.read(path);Map<String, String> map = new HashMap<String, String>();map.put("ns", "http://www.springframework.org/schema/beans");XPath xPath = doc.createXPath("//ns:beans/ns:bean");xPath.setNamespaceURIs(map);List<Element> nodes = xPath.selectNodes(doc);for (Element node : nodes) {String id = node.attributeValue("id");String className = node.attributeValue("class");ScoreRule scoreRule = new ScoreRule(id, className);scoreRuleList.add(scoreRule);} }public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}}

3,在ZhouClassPathXmlApplicationContext中,有重要的如下构造函数,在这构造函数中根据配置文件名称(applicationContext.xml)作为参数。

       readXml()读取applicationContext.xml中的bean,并放入list中;

       instance()遍历list中的bean,并通过反射获取id和class文件,放入map中封装实例化之后的bean对象。

         //构造函数public ZhouClassPathXmlApplicationContext(String fileName) throws Exception {this.readXml(fileName);//读取this.instance();//初始化//this.injectObjec();//注入bean}

4,在ZhouClassPathXmlApplicationContext中,还必须提供getBean方法,是外界获取实例化bean的入口:

         //提供获取bean的方法public Object getBean(String beanName) {return beans.get(beanName);}

5,当然了,bean肯定是少不了的了:

public class ScoreRule {private String id;private String className;  public ScoreRule(){}public ScoreRule(String id, String className) {this.id = id;this.className = className;} public String getScore(){return "读取分数!";}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;}

6,测试类Spring_instanceBean:

package junit.test;import org.junit.Test;import cn.zhou.ScoreRule;import cn.zhou.spring.ZhouClassPathXmlApplicationContext;public class Spring_instanceBean {/** * 模拟spring实例化bean * @throws Exception  */@Testpublic void instanceBean() throws Exception{ZhouClassPathXmlApplicationContext context = new ZhouClassPathXmlApplicationContext("applicationContext_zhou.xml");ScoreRule bean = (ScoreRule)context.getBean("ScoreRule");String score = bean.getScore();System.out.println(score);}}

7,以上就是模拟spring容器来读取和实例化bean的实例,以后我们就了解了spring是怎么来管理bean了!

0 0
原创粉丝点击