博客
关于我
2-Spring IoC实现之DefaultListableBeanFactory(Spring Boot2)
阅读量:324 次
发布时间:2019-03-04

本文共 3260 字,大约阅读时间需要 10 分钟。

Spring IoC作用是管理Bean的生命周期并提供用户使用bean,所以我们有必要来看看Spring如何管理这些Bean的,前面我们提到IoC基本功能由BeanFactory系列类型实现,BeanFactory系列中就是DefaultListableBeanFactory这个类型完成的IoC基本功能。先来看下继承体系:

我们需要理解的是,Spring在启动时,整个流程是怎样的。从大体流程上,我们应该可以预料到,肯定是ApplicationContext方法先被调用,然后才是BeanFactory方法被调用,但是主要操作上肯定是先BeanFactory操作,再是ApplicationContext。也就是说ApplicationContext是入口,他肯定会调用BeanFactory进行操作的。下面我们来看看具体主要流程。

我们知道ApplicationContext在context包中的唯一子接口是ConfigurableApplicationContext,这个接口中有一个方法

/** * Load or refresh the persistent representation of the configuration, * which might an XML file, properties file, or relational database schema. * 

As this is a startup method, it should destroy already created singletons * if it fails, to avoid dangling resources. In other words, after invocation * of that method, either all or no singletons at all should be instantiated. * @throws BeansException if the bean factory could not be initialized * @throws IllegalStateException if already initialized and multiple refresh * attempts are not supported */void refresh() throws BeansException, IllegalStateException;

上面的注释翻一下来的意思是,加载或刷新持久化的配置描述,这个描述可以是一个xml文件,properties文件或者相关的数据库模式。他作为一个启动方法,在失败时他必须销毁已经创建的单例beans,以防资源被挂起。换句话说,这个方法被调用后,要么全部的单例被创建要么一个也不被创建。这个注释应该是比较片面的,我们只能获取到一部分信息。只要知道他是启动方法就行了。

AbstractApplicationContext是实现ConfigurableApplicationContext的抽象类,里面实现了refresh()方法,所有其他实现了ApplicationContext接口的实现类型都会调用这个方法来启动一个IoC容器,并且我们知道,AbstractApplicationContext没有持有DefaultListableBeanFactory成员变量,我们来看看这个方法,AbstractApplicationContext#refresh()

@Overridepublic void refresh() throws BeansException, IllegalStateException {	synchronized (this.startupShutdownMonitor) {		// Prepare this context for refreshing.		prepareRefresh();		// Tell the subclass to refresh the internal bean factory.		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();		// Prepare the bean factory for use in this context.		prepareBeanFactory(beanFactory);		try {			// Allows post-processing of the bean factory in context subclasses.			postProcessBeanFactory(beanFactory);			// Invoke factory processors registered as beans in the context.			invokeBeanFactoryPostProcessors(beanFactory);			// Register bean processors that intercept bean creation.			registerBeanPostProcessors(beanFactory);			// Initialize message source for this context.			initMessageSource();			// Initialize event multicaster for this context.			initApplicationEventMulticaster();			// Initialize other special beans in specific context subclasses.			onRefresh();			// Check for listener beans and register them.			registerListeners();			// Instantiate all remaining (non-lazy-init) singletons.			finishBeanFactoryInitialization(beanFactory);			// Last step: publish corresponding event.			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();		}	}}

 

转载地址:http://mvzh.baihongyu.com/

你可能感兴趣的文章
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>