笨鸟之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
- 笨鸟之Spring实例化bean的模拟实现
- Spring之实例化Bean
- 模拟实现spring的bean工厂
- SPRING BEAN的实例化
- spring Bean的实例化
- Spring的Bean实例化
- Spring-Bean的实例化
- Spring系列--bean容器的实例化之构造器创建bean实例(采用dtd约束)
- Spring学习笔记之Bean的实例化
- Spring学习之实例化bean的三种方式
- Spring入门之实例化Bean的三种方式
- spring之bean的三种实例化方式
- Spring学习之bean的实例化,销毁
- Spring之Bean容器初始化以及bean实例的获取
- spring的bean管理之bean实例化三种方式
- 模拟spring,自定义容器一(属性注入、Bean的实例化)
- Spring的bean管理(xml方式)之Bean实例化的方式
- Spring 实例化bean
- bzoj1037:[ZJOI2008]生日聚会Party[DP]
- MIPS 架构 Linux driver编译时遇到的问题
- 疯狂程序员之“始”
- 安卓有关广播接收者(BroadCastReceive)的知识
- 我的近况
- 笨鸟之Spring实例化bean的模拟实现
- STL笔记之空间配置器
- 环境: ubuntu 12.04 编译安装 osgEarth
- 在远程连接时提示“您的凭据不工作”怎么办?
- 笨鸟之Struts2、Spring、Mybatis和Oracle的联调
- rt5350 两路 sonix sn9c291 同时直播成功
- 工作
- Maven 坐标与依赖管理
- phonegap download() 下载路径问题