Spring源码--01--Spring IOC简述
Posted 高高for 循环
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring源码--01--Spring IOC简述相关的知识,希望对你有一定的参考价值。
IOC 流程图
流程步骤:
- 创建beanFactory容器
- 加载配置文件,解析bean定义信息,包装成BeanDefinition
- 执行BeanFactoryPostProcessor
- 准备BeanPostProcessor,广播器,监听器
- 实例化操作
- BeanPostProcessor-before
- 初始化操作
- BeanPostProcessor-after
- 获取对象
Demo
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.6</version>
</dependency>
User
package springdemo.cy.bean;
public class User {
private String userId;
private String userName;
public User() {
}
public User(String userId, String userName) {
this.userId = userId;
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\\'' +
", userName='" + userName + '\\'' +
'}';
}
}
user.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.xsd">
<bean id="user1" class="springdemo.cy.bean.User">
<property name="userId" value="0001"></property>
<property name="userName" value="张三"></property>
</bean>
<bean id="user2" class="springdemo.cy.bean.User">
<constructor-arg name="userId" value="0002"></constructor-arg>
<constructor-arg name="userName" value="李四"></constructor-arg>
</bean>
</beans>
test
package springdemo.cy.sp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import springdemo.cy.bean.User;
public class test {
public static void main(String[] args) {
ApplicationContext appLication = new ClassPathXmlApplicationContext("user.xml");
Object bean = appLication.getBean("user1");
User user = (User) bean;
System.out.println(user);
}
}
Debug分析
1.启动类:ClassPathXmlApplicationContext
2.ClassPathXmlApplicationContext
构造方法
进入refres()
3.AbstractApplicationContext
prepareRefresh(); 做一些前期准备工作
AbstractApplicationContext
4.AbstractRefreshableApplicationContext
new 一个 DefaultListableBeanFactory
加载bean的定义信息
beandefinitionMap
一路返回到 3.AbstractApplicationContext
对BeanFactory做一些初始化设置
留给子类做扩展用的方法
执行BeanFactoryPostProcessor 执行器
实例化所有的剩下的非懒加载的单例对象
5.DefaultListableBeanFactory
6.AbstractBeanFactory
重要的判断 三级缓存是否有对象的方法 getSingleton()
/**
* Return the (raw) singleton object registered under the given name.
* <p>Checks already instantiated singletons and also allows for an early
* reference to a currently created singleton (resolving a circular reference).
* @param beanName the name of the bean to look for
* @param allowEarlyReference whether early references should be created or not
* @return the registered singleton object, or {@code null} if none found
*/
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//从一级缓存获取
Object singletonObject = this.singletonObjects.get(beanName);
//如果一级缓存中没有,并且已标记这个bean正在被定义
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
synchronized (this.singletonObjects) {
//从二级缓存获取bean
singletonObject = this.earlySingletonObjects.get(beanName);
//如果二级缓存也拿不到 去三级缓存拿
if (singletonObject == null && allowEarlyReference) {
//从三级缓存取值
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
//如果三级缓存存在值,将三级缓存中的缓存移除,加入二级缓存
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
return singletonObject;
}
然后进行一系列判断 ,直到createBean()方法,
再点进去
7.AbstractAutowireCapableBeanFactory
跳过一系列校验方法,知道do开头的重要方法
doCreateBean(beanName, mbdToUse, args);
点进去createBeanInstance方法
进入instantiateBean(beanName, mbd);
进入 instantiate()方法
8.SimpleInstantiationStrategy
进入BeanUtils
9.BeanUtils
ctor.newInstance(argsWithDefaultValues)
通过反射创建目标对象
获取对象后一直返回,直到
AbstractAutowireCapableBeanFactory
7.AbstractAutowireCapableBeanFactory
DI注入 设置对象属性值
populateBean(beanName, mbd, instanceWrapper);
进入 initializeBean( ) 初始化bean对象
一系列返回,到 DefaultListableBeanFactory类
5.DefaultListableBeanFactory
流程再走一遍,最后到AbstractApplicationContext类中
以上是关于Spring源码--01--Spring IOC简述的主要内容,如果未能解决你的问题,请参考以下文章
Spring Day01 Spring 框架概述以及Spring中基于XML的IOC配置
Spring4- 01 - Spring框架简介及官方压缩包目录介绍- Spring IoC 的概念 - Spring hello world环境搭建