Spring源码加载过程图解(一)
最近看了一下Spring源码加载的简装版本,为了更好的理解,所以在绘图的基础上,进行了一些总结。(图画是为了理解和便于记忆Spring架构)
Spring的核心是IOC(控制反转)和AOP(面向切面编程),首先我们先一步一步的来了解IOC的实现:
一:首先是第一个模型:Model1:
种子 = BeanDefinition 工厂=BeanFactory
1、此处我们可以看见两个对象 BeanDefinition和BeanFactory,此处把创建对象的权限交给了BeanFactory,现在我们可以看到IOC的雏形,即把创建对象的权利交给了工厂。
public class BeanDefinition {
private Object bean;
public BeanDefinition(Object bean) {
this.bean = bean;
}
public Object getBean() {
return bean;
}
}
public class BeanFactory {
private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
public Object getBean(String name) {
return beanDefinitionMap.get(name).getBean();
}
public void registerBeanDefinition(String name, BeanDefinition beanDefinition) {
beanDefinitionMap.put(name, beanDefinition);
}
}
二:接下來我們看第二个模型,对BeanFactory进行扩展
1、此处的BeanDefinition 进行了扩展字段,可以看做是种子有了 3 片叶子,便于记忆,分别是 :
Object -> bean Class ->beanClass String ->beanClassName
2、此处的BeanFactory有了解耦的思想,使用了接口和抽象类
public class BeanDefinition {
private Object bean;
private Class beanClass;
private String beanClassName;
public BeanDefinition() {
}
public void setBean(Object bean) {
this.bean = bean;
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public String getBeanClassName() {
return beanClassName;
}
public void setBeanClassName(String beanClassName) {
this.beanClassName = beanClassName;
try {
this.beanClass = Class.forName(beanClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Object getBean() {
return bean;
}
}
public interface BeanFactory {
Object getBean(String name);
void registerBeanDefinition(String name, BeanDefinition beanDefinition);
}
此处 BeanFactory定义了获取Bean 和注册Bean的方法
public abstract class AbstractBeanFactory implements BeanFactory {
private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
@Override
public Object getBean(String name) {
return beanDefinitionMap.get(name).getBean();
}
@Override
public void registerBeanDefinition(String name, BeanDefinition beanDefinition) {
Object bean = doCreateBean(beanDefinition);
beanDefinition.setBean(bean);
beanDefinitionMap.put(name, beanDefinition);
}
/**
* 初始化bean
* @param beanDefinition
* @return
*/
protected abstract Object doCreateBean(BeanDefinition beanDefinition);
}
此处实现了getBean 和registerBean的方法;另外声明了 doCreateBean的抽象方法;
public class AutowireCapableBeanFactory extends AbstractBeanFactory {
@Override
protected Object doCreateBean(BeanDefinition beanDefinition) {
try {
Object bean = beanDefinition.getBeanClass().newInstance();
return bean;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
最后实现类通过反射的方式,创建bean;并注册到工厂里,以便调用。
三:接下来继续完善,查看第三个模型:
1、此处 BeanDefiniton多了一个自定义的属性 propertyValues,便于属性的注入,BeanFactory方法也有所改变
1、BeanDefinition类的定义
public class BeanDefinition {
private Object bean;
private Class beanClass;
private String beanClassName;
private PropertyValues propertyValues;
public BeanDefinition() {
}
public void setBean(Object bean) {
this.bean = bean;
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
public String getBeanClassName() {
return beanClassName;
}
public void setBeanClassName(String beanClassName) {
this.beanClassName = beanClassName;
try {
this.beanClass = Class.forName(beanClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Object getBean() {
return bean;
}
public PropertyValues getPropertyValues() {
return propertyValues;
}
public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
}
/**
* 用于bean的属性注入
* @author yihua.huang@dianping.com
*/
public class PropertyValue { private final String name; private final Object value; public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
} public String getName() {
return name;
} public Object getValue() {
return value;
}
}
import java.util.ArrayList;
import java.util.List; /**
* 包装一个对象所有的PropertyValue。<br/>
* 为什么封装而不是直接用List?因为可以封装一些操作。
* @author yihua.huang@dianping.com
*/
public class PropertyValues { private final List<PropertyValue> propertyValueList = new ArrayList<PropertyValue>(); public PropertyValues() {
} public void addPropertyValue(PropertyValue pv) {
//TODO:这里可以对于重复propertyName进行判断,直接用list没法做到
this.propertyValueList.add(pv);
} public List<PropertyValue> getPropertyValues() {
return this.propertyValueList;
} }
2、BeanFactory的定义
/**
* bean的容器
* @author yihua.huang@dianping.com
*/
public interface BeanFactory { Object getBean(String name); void registerBeanDefinition(String name, BeanDefinition beanDefinition) throws Exception;
}
/**
* @author yihua.huang@dianping.com
*/
public abstract class AbstractBeanFactory implements BeanFactory { private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(); @Override
public Object getBean(String name) {
return beanDefinitionMap.get(name).getBean();
} @Override
public void registerBeanDefinition(String name, BeanDefinition beanDefinition) throws Exception {
Object bean = doCreateBean(beanDefinition);
beanDefinition.setBean(bean);
beanDefinitionMap.put(name, beanDefinition);
} /**
* 初始化bean
* @param beanDefinition
* @return
*/
protected abstract Object doCreateBean(BeanDefinition beanDefinition) throws Exception; }
/**
* 可自动装配内容的BeanFactory
*
* @author yihua.huang@dianping.com
*/
public class AutowireCapableBeanFactory extends AbstractBeanFactory { @Override
protected Object doCreateBean(BeanDefinition beanDefinition) throws Exception {
Object bean = createBeanInstance(beanDefinition);
applyPropertyValues(bean, beanDefinition);
return bean;
} protected Object createBeanInstance(BeanDefinition beanDefinition) throws Exception {
return beanDefinition.getBeanClass().newInstance();
} protected void applyPropertyValues(Object bean, BeanDefinition mbd) throws Exception {
for (PropertyValue propertyValue : mbd.getPropertyValues().getPropertyValues()) {
Field declaredField = bean.getClass().getDeclaredField(propertyValue.getName());
declaredField.setAccessible(true);
declaredField.set(bean, propertyValue.getValue());
}
}
}
此处增加了添加 属性的方法 applyPropertyValues;
此模型可概括为:
#1.step1-最基本的容器
*step-1-container-register-and-get*
单纯的map,有get和put bean的功能
# 2.step2-将bean创建放入工厂
*step-2-abstract-beanfactory-and-do-bean-initilizing-in-it*
1. 抽象beanfactory
2. 将bean初始化放入beanfactory
# 2.step3-为bean注入属性
*step-3-inject-bean-with-property*
4、接下来我们来看第四个模型,Spring是如何实现读取配置文件的bean属性的,Model4:

从图中可以看出多了几个模块,现在分别来介绍一下:
1、BD 是简化绘制版的 BeanDefiniton
2、UR 对应下面 IO类,用于读取xml文件
RL 是ResourceLoader,用于配置文件路径
3、BDR 是用来解析XML文件的模块类
4、最后交由BeanFactory创造bean,并提供bean.
1、BeanDefinition
BeanDefiniton
/**
* bean的内容及元数据,保存在BeanFactory中,包装bean的实体
*
* @author yihua.huang@dianping.com
*/
public class BeanDefinition { private Object bean; private Class beanClass; private String beanClassName; private PropertyValues propertyValues = new PropertyValues(); public BeanDefinition() {
} public void setBean(Object bean) {
this.bean = bean;
} public Class getBeanClass() {
return beanClass;
} public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
} public String getBeanClassName() {
return beanClassName;
} public void setBeanClassName(String beanClassName) {
this.beanClassName = beanClassName;
try {
this.beanClass = Class.forName(beanClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public Object getBean() {
return bean;
} public PropertyValues getPropertyValues() {
return propertyValues;
} public void setPropertyValues(PropertyValues propertyValues) {
this.propertyValues = propertyValues;
}
PropertyValue
/**
* 用于bean的属性注入
* @author yihua.huang@dianping.com
*/
public class PropertyValue { private final String name; private final Object value; public PropertyValue(String name, Object value) {
this.name = name;
this.value = value;
} public String getName() {
return name;
} public Object getValue() {
return value;
}
}
PropertyValues
/**
* 包装一个对象所有的PropertyValue。<br/>
* 为什么封装而不是直接用List?因为可以封装一些操作。
* @author yihua.huang@dianping.com
*/
public class PropertyValues { private final List<PropertyValue> propertyValueList = new ArrayList<PropertyValue>(); public PropertyValues() {
} public void addPropertyValue(PropertyValue pv) {
//TODO:这里可以对于重复propertyName进行判断,直接用list没法做到
this.propertyValueList.add(pv);
} public List<PropertyValue> getPropertyValues() {
return this.propertyValueList;
} }
2、IO类
Resource
/**
* Resource是spring内部定位资源的接口。
* @author yihua.huang@dianping.com
*/
public interface Resource { InputStream getInputStream() throws IOException;
}
/**
* @author yihua.huang@dianping.com
*/
public class UrlResource implements Resource { private final URL url; public UrlResource(URL url) {
this.url = url;
} @Override
public InputStream getInputStream() throws IOException{
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
return urlConnection.getInputStream();
}
}
ResourceLoader
/**
* @author yihua.huang@dianping.com
*/
public class ResourceLoader { public Resource getResource(String location){
URL resource = this.getClass().getClassLoader().getResource(location);
return new UrlResource(resource);
}
}
3、BeanDefinitionReader
/**
* 从配置中读取BeanDefinitionReader
* @author yihua.huang@dianping.com
*/
public interface BeanDefinitionReader { void loadBeanDefinitions(String location) throws Exception;
}
/**
* 从配置中读取BeanDefinitionReader
*
* @author yihua.huang@dianping.com
*/
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader { private Map<String,BeanDefinition> registry; private ResourceLoader resourceLoader; protected AbstractBeanDefinitionReader(ResourceLoader resourceLoader) {
this.registry = new HashMap<String, BeanDefinition>();
this.resourceLoader = resourceLoader;
} public Map<String, BeanDefinition> getRegistry() {
return registry;
} public ResourceLoader getResourceLoader() {
return resourceLoader;
}
}
XmlBeanDefinitionReader
/**
* @author yihua.huang@dianping.com
*/
public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader { public XmlBeanDefinitionReader(ResourceLoader resourceLoader) {
super(resourceLoader);
} @Override
public void loadBeanDefinitions(String location) throws Exception {
InputStream inputStream = getResourceLoader().getResource(location).getInputStream();
doLoadBeanDefinitions(inputStream);
} protected void doLoadBeanDefinitions(InputStream inputStream) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(inputStream);
// 解析bean
registerBeanDefinitions(doc);
inputStream.close();
} public void registerBeanDefinitions(Document doc) {
Element root = doc.getDocumentElement(); parseBeanDefinitions(root);
} protected void parseBeanDefinitions(Element root) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
processBeanDefinition(ele);
}
}
} protected void processBeanDefinition(Element ele) {
String name = ele.getAttribute("name");
String className = ele.getAttribute("class");
BeanDefinition beanDefinition = new BeanDefinition();
processProperty(ele,beanDefinition);
beanDefinition.setBeanClassName(className);
getRegistry().put(name, beanDefinition);
} private void processProperty(Element ele,BeanDefinition beanDefinition) {
NodeList propertyNode = ele.getElementsByTagName("property");
for (int i = 0; i < propertyNode.getLength(); i++) {
Node node = propertyNode.item(i);
if (node instanceof Element) {
Element propertyEle = (Element) node;
String name = propertyEle.getAttribute("name");
String value = propertyEle.getAttribute("value");
beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name,value));
}
}
}
}
此文件用来定义文名,并解析xml文件
未完,待续 。。。。。。
Spring源码加载过程图解(一)的更多相关文章
- Spring源码加载BeanDefinition过程
本文主要讲解Spring加载xml配置文件的方式,跟踪加载BeanDefinition的全过程. 源码分析 源码的入口 ClassPathXmlApplicationContext构造函数 new C ...
- Spring源码-加载和IOC部分
源代码和注释放在了github上,包括加载过程的注释和getBean部分的 地址: https://github.com/lvxingzhi/spring-framework-4.3.9-note.g ...
- 工厂模式模拟Spring的bean加载过程
一.前言 在日常的开发过程,经常使用或碰到的设计模式有代理.工厂.单例.反射模式等等.下面就对工厂模式模拟spring的bean加载过程进行解析,如果对工厂模式不熟悉的,具体可以先去学习一下工厂 ...
- Spring Bean 的加载过程
Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...
- Spring源码:Spring IoC容器加载过程(1)
Spring源码版本:4.3.23.RELEASE 一.加载过程概览 Spring容器加载过程可以在org.springframework.context.support.AbstractApplic ...
- Spring源码:Spring IoC容器加载过程(2)
Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...
- Spring IOC bean加载过程
首先我们不要在学习Spring的开始产生畏难情绪.Spring没有臆想的那么高深,相反,它帮我们再项目开发中制定项目框架,简化项目开发.它的主要功能是将项目开发中繁琐的过程流程化,模式化,使用户仅在固 ...
- 【学习底层原理系列】重读spring源码3-加载beanDefinition的方法obtainFreshBeanFactory
obtainFreshBeanFactory()方法概述 定义BeanFactory,并加载以下两种bean的定义,装配到BeanFactory: 1.配置文件中定义的bean 2.通过<con ...
- spring的BeanFactory加载过程
ApplicationContext spring = new ClassPathXmlApplicationContext("classpath*:spring/applicationCo ...
随机推荐
- Why ZK
ZooKeeper是一个开放源代码的分布式协调服务,由知名互联网公司雅虎创建,是Google Chubby的开源实现.ZooKeeper的设计目标是将那些复杂且容易出错的分布式一致性服务封装起来,构成 ...
- 向量点积(Dot Product),向量叉积(Cross Product)
参考的是<游戏和图形学的3D数学入门教程>,非常不错的书,推荐阅读,老外很喜欢把一个东西解释的很详细. 1.向量点积(Dot Product) 向量点积的结果有什么意义?事实上,向量的点积 ...
- 如何使用fiddller跟踪windows进程发送的请求20140911
总结点:如何使用fiddller跟踪windows进程发送的请求 案例如下: 需求:运维工具提出需求,对每个插件的配置文件,同步到运维中心时先加密,然后传输到运维中心解密,存储到数据库 测试分析:这个 ...
- BZOJ4518 Sdoi2016 征途 【斜率优化DP】 *
BZOJ4518 Sdoi2016 征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m ...
- 安装WampServer关闭mysql服务后打不开了
WampServer自带了mysql精简班的数据库了 WampServer自带 的mysql和你独立安装的mysql端口号冲突了 mysql默认的端口号是3306 建议你修改WampServer的数据 ...
- matlab 中的function定义. 用最简单的例子说明.
function y=myfunction(a,b)其中a,b是输入函数的参数,y是函数返回的值.当需要返回多个值时,可以将y看作一个数组,或者直接将函数的开头写成如function [x,y]=my ...
- [LeetCode系列]N皇后问题递归解法 -- 位操作方式
N皇后问题: 给定8*8棋盘, 放置n个皇后, 使其互相不能攻击(即2个皇后不能放在同一行/列/正反对角线上), 求解共有多少种放置方式? 这个问题的解答网上有不少, 但是位操作解法的我看到的不多. ...
- oracle之 oracle database vault(数据库保险库)
在12c建库中 Database Vault 与 Label Security 选项,之前没有留意过,特意记录一下 12.1 中: 12.2 中: 转载:http://www.linuxidc.co ...
- ecmall页面空白解决方案(转)
页面空白解决方案: ------------------------------------------------------------------------------------------ ...
- linux误删数据恢复
linux下数据恢复工具有: 1.通过分析文件系统的日志,解析文件的inode,可以恢复ex3 ex4的文件系统下的数据 extundelete:扫描inode和恢复数据同时进行,因此恢复速度很快.支 ...