拉勾笔记02(2)Spring IOC和AOP源码篇
拉勾笔记02(2)Spring IOC和AOP源码篇
本篇文章是基于拉勾高薪课程学习笔记,基础是在Spring源码编译执行,jdk源码编译教程请自行搜索
1、IoC容器初始化流程
1.1、容器继承体系
ApplicationContext是容器的高级接口,BeanFactory是顶级容器,规范/定义容器的基础行为
Spring应用上下文,称之为IoC容器,是一组组件和过程的集合,包括BeanFactory、单例池、BeanPostProcessor等之间的协作流程
首先看下BeanFactory的继承结构体系图,ApplicationContext除了继承BeanFactory,还继承了MessageSource、ResourceLoader等接口
上图是ApplicationContext继承的ListableBeanFactory接口的方法列表,确实有好多方法是顶级接口BeanFactory中所没有的
1.2、关键方法refresh()
通过参照Bean生命周期Debug可知,无论是Bean构造器、Bean后置处理器或者Bean工厂后置处理器初始化,都会调用refresh方法
废话少说,直接上源码
@Override
public void refresh() throws BeansException, IllegalStateException {
// 对象锁加锁
synchronized (this.startupShutdownMonitor) {
/*
Prepare this context for refreshing.
刷新前的预处理
表示在真正做refresh操作之前需要准备做的事情:
设置Spring容器的启动时间,
开启活跃状态,撤销关闭状态
验证环境信息里一些必须存在的属性等
*/
prepareRefresh();
/*
Tell the subclass to refresh the internal bean factory.
获取BeanFactory;默认实现是DefaultListableBeanFactory
加载BeanDefition 并注册到 BeanDefitionRegistry
*/
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
/*
Prepare the bean factory for use in this context.
BeanFactory的预准备工作(BeanFactory进行一些设置,比如context的类加载器等)
*/
prepareBeanFactory(beanFactory);
try {
/*
Allows post-processing of the bean factory in context subclasses.
BeanFactory准备工作完成后进行的后置处理工作
*/
postProcessBeanFactory(beanFactory);
/*
Invoke factory processors registered as beans in the context.
实例化实现了BeanFactoryPostProcessor接口的Bean,并调用接口方法
*/
invokeBeanFactoryPostProcessors(beanFactory);
/*
Register bean processors that intercept bean creation.
注册BeanPostProcessor(Bean的后置处理器),在创建bean的前后等执行
*/
registerBeanPostProcessors(beanFactory);
/*
Initialize message source for this context.
初始化MessageSource组件(做国际化功能;消息绑定,消息解析);
*/
initMessageSource();
/*
Initialize event multicaster for this context.
初始化事件派发器
*/
initApplicationEventMulticaster();
/*
Initialize other special beans in specific context subclasses.
子类重写这个方法,在容器刷新的时候可以自定义逻辑;如创建Tomcat,Jetty等WEB服务器
*/
onRefresh();
/*
Check for listener beans and register them.
注册应用的监听器。就是注册实现了ApplicationListener接口的监听器bean
*/
registerListeners();
/*
Instantiate all remaining (non-lazy-init) singletons.
初始化所有剩下的非懒加载的单例bean
初始化创建非懒加载方式的单例Bean实例(未设置属性)
填充属性
初始化方法调用(比如调用afterPropertiesSet方法、init-method方法)
调用BeanPostProcessor(后置处理器)对实例bean进行后置处理
*/
finishBeanFactoryInitialization(beanFactory);
/*
Last step: publish corresponding event.
完成context的刷新。主要是调用LifecycleProcessor的onRefresh()方法,并且发布事件(ContextRefreshedEvent)
*/
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
首先通过find usage反调来查看第一行synchronized对象锁都被哪些方法调用
由图可知,该锁的作用,就是在refresh和close方法不能同时运行,避免冲突
因为refresh方法中调用的方法过多,我下面就挑重要的方法展开,如果需要可以自行查看源码
/*
Tell the subclass to refresh the internal bean factory.
获取BeanFactory;默认实现是DefaultListableBeanFactory
加载BeanDefition 并注册到 BeanDefitionRegistry
*/
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
1.3、BeanFactory获取子流程
调用栈如下:
refresh——obtainFreshBeanFactory——refreshBeanFactory
@Override
protected final void refreshBeanFactory() throws BeansException {
// 判断是否已有bean factory
if (hasBeanFactory()) {
// 销毁 beans
destroyBeans();
// 关闭 bean factory
closeBeanFactory();
}
try {
// 实例化 DefaultListableBeanFactory
DefaultListableBeanFactory beanFactory = createBeanFactory();
// 设置序列化id
beanFactory.setSerializationId(getId());
// 自定义bean工厂的一些属性(是否覆盖、是否允许循环依赖)
customizeBeanFactory(beanFactory);
// 加载应用中的BeanDefinitions
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
// 赋值当前bean facotry
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
时序图如下:
1.4、BeanDefinition加载注册子流程
调用栈如下:
obtainFreshBeanFactory——refreshBeanFactory——loadBeanDefinitions
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// 给指定的beanFactory创建一个XmlBeanDefinitionReader读取器对象,用于读取解析xml对象
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// 给XmlBeanDefinitionReader对象设置一些context上下文中的环境属性
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// 提供给子类实现提供一些自定义的初始化策略
initBeanDefinitionReader(beanDefinitionReader);
// 真正的去加载BeanDefinitions
loadBeanDefinitions(beanDefinitionReader);
}
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
// 从Resource资源对象加载BeanDefinitions
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
// 从xml配置文件加载BeanDefinition对象
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
1.5、Bean对象创建流程
@Override
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
logger.trace("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
// 所有bean的名字
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
// 触发所有非延迟加载单例bean的初始化,主要步骤为getBean
for (String beanName : beanNames) {
// 合并父BeanDefinition对象
// map.get(beanName)
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
// 如果是FactoryBean则加&
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
else {
// 实例化当前bean
getBean(beanName);
}
}
}
...
}
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
// 解析beanName 如果以&开头去掉&开头,如果是别名获取到真正的名字
final String beanName = transformedBeanName(name);
Object bean;
// 单纯理解尝试从缓存中获取 bean
Object sharedInstance = getSingleton(beanName);
// 如果已经存在则返回
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
// 针对 FactoryBean 的处理
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// 如果是prototype类型且开启允许循环依赖,则抛出异常
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
// 检查父工厂中是否存在该对象
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
// 合并父子bean 属性
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
// 处理dependsOn配置
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// 创建单例bean
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
// 创建 bean
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
}
1.6、lazy-init延迟加载
1.7、Spring IoC循环依赖
是什么
循环依赖指的是循环引用,也就是两个或两个以上的Bean互相持有对方,形成闭环,如下图
原理
为了解决循环依赖问题,Spring是以三级缓存的机制来处理的,其实官方文档并没有三级缓存这样的定义,在Spring中仅仅是三个Map。其中一级缓存singletonObjects就是咱们熟知的单例池,二级缓存singleFactories是映射创建Bean的原始工厂,三级缓存earlySingletonObjects是映射Bean的早期引用。
<!--循环依赖问题-->
<bean id="aBean" class="com.lagou.edu.aBean">
<property name="bBean" ref="bBean"/>
</bean>
<bean id="bBean" class="com.lagou.edu.bBean">
<property name="aBean" ref="aBean"/>
</bean>
上例就是一个简单的循环依赖,A依赖于B,B依赖于A。Spring的解决方式如下
- A在对象实例化后放入三级缓存(提前暴露)
- B在创建的过程中发现依赖于A,那么先去一级缓存中查找,没有再去二级,还没有再去三级。
- 在三级缓存中查找到后会升级到二级缓存。
- 此时B创建完成,则将成型的B放入一级缓存中。
- 这时候A在创建的过程中会使用一级缓存中的B完成创建
三级缓存的源码过于复杂,这里只放一个时序图