IOC(Inversion of Control)和DI(Dependecy Injection),IOC控制反转,由Spring来控制管理对象的生命周期和对象间的关系。将对象交给容器来创建,程序不再创建;DI依赖注入,容器创建对象时,容器将对象的依赖注入到对象中。Spring以这两种思想来实现对Bean的管理。继而实现松耦合。
首先了解一下关键API
BeanFactory,接口,Spring Bean容器顶级接口,提供访问Bean容器的的基本接口。类和接口的关系如下:
ApplicationContext,接口,为应用提供配置的核心接口。类和结构的关系如下。
通常,我们启动Spring容器就是初始化一个ApplicationContext的实例,比如new ClassPathXmlApplicationContext();随着ApplicationContext的实例化,Spring容器也相应的创建。所以下来通过ApplicationContext的实例化来了解Spring容器的启动过程。
同时,SpringBoot应用启动的API,SpringApplication.run(),在启动的过程中,根据参数args,初始化不同的ApplicationContext实现,源码如下:
protected ConfigurableApplicationContext createApplicationContext() {
Class> contextClass = this.applicationContextClass;
//首先根据配置,没有配置取默认的ApplicationContext
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
//加载AnnotationConfigServletWebServerApplicationContext
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
//加载AnnotationConfigServletWebServerApplicationContext
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
//加载AnnotationConfigApplicationContext
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass",
ex);
}
}
//创建ApplicationContext实例
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
默认的AnnotationConfigApplicationContext是基于注解的,基于注解的配置也被大多数人所接受,所以我们通过实例化AnnotationConfigApplicationContext来启动Spring容器,进一步了解Spring。
启动Spring容器测试以及
public class SpringTest {
public static void main(String[] args) {
//初始化Spring容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//扫描包com.weihao
applicationContext.scan("com.weihao");
//加载配置
applicationContext.refresh();
//通过ApplicationContext获取TestService实例
TestService testService = (TestService) applicationContext.getBean("testService");
//调用TestService方法
testService.sayHello();
}
}
TestService源码
@Component
public class TestService {
public void sayHello(){
System.out.println("hello world");
}
}
接下来看一下AnnotationConfigApplicationContext 启动时,都做了那些事情。AnnotationConfigApplicationContext源码构造器。
//创建一个AnnotationConfigApplicationContext, 需要调用#register填充,手动调用#refresh刷新
public AnnotationConfigApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
构造器中AnnotationConfigApplicationContext使用自己作为参数,初始化了AnnotatedBeanDefinitionReader和ClassPathBeanDefinitionScanner两个对象,并传给成员变量。其中AnnotationConfigApplicationContext实现了BeanDefinitionRegistry接口,作为registry对象去初始化两个成员变量。AnnotatedBeanDefinitionReader根据文档,功能是注册基于注解的bean的,ClassPathBeanDefinitionScanner是在classpath扫描BeanDefinition,注册扫描到的BeanDefinition。
BeanDefinition,顾名思义,就是Bean的定义。
其中AnnotatedBeanDefinitionReader的构造函数中,将AnnotationConfigApplicationContext对象作为registry成员属性,新创建了ConditionEvaluator,并给registry注册相关处理器。
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
//@Conditional注解的解析评估
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
//将注解配置处理器注册到Registry
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
ConditionEvaluator,用来解析评估@Conditional注解。
在给定的Registry中注册相关注解配置处理器。处理逻辑如下。
public static Set registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {
//取出BeanFactory,AnnotationConfigApplicationContext继承自GenericApplicationContext,
//具有DefaultListableBeanFactory的属性。
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {
// 依赖比较器
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
// 自动装配解析器
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
}
// Bean定义管理器集合
Set beanDefs = new LinkedHashSet<>(8);
// @Configuration注解的引导处理器,按照优先级排序的,因为在@Configuration类中声明的任何
// Bean方法,都必须在其他类之前将BeanDefinition注册到Registry
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//1, 自动装备带注解的字段、setter方法、任意配置方法,要注入的这些成员都是通过Java5注解检测,
// 默认情况下Spring的@Autowire注解和@Value注解,如果可用,也支持JSR-330的注解,@Inject
//2,任何给定bean类,只有一个构造函数是,可以声明这个注解,且require参数设置为true,指示
//构造函数自动装备Bean。如果有多个构造函数,他们被当做自动装备候选,有最多依赖的构造函数且
//可以在Spring容器中匹配所有依赖的构造函数,会被选择为自动装备构造函数。如果没有一个候选构造
//函数满足,会选择默认的构造函数,如果类只声明了一个构造函数,即使没有注解也会使用被使用。
//带注解的构造函数不需要声明为public
if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//检查支持JSR-250, 如果类的路径中包含JSR-250注解,添加CommonAnnotationBeanPostProcessor
//1,支持开箱即用的Java注解,JSR-250的注解,即javax.annotation包中的注解
//2,同时继承了InitDestroyAnnotationBeanPostProcessor,支持@PostConstruct,@PreDestroy
//3,支持JAX-WS注解,EJB3注解,可以同时指定本地名称和全局JNDI检索名称
//4,对于直接的JNDI访问,使用JavaEE应用的java:/comp/env命名空间中的JNDI资源引用,来解析
//资源名称
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//检查JPA支持, 如果包含,增加PersistenceAnnotationBeanPostProcessor.
//处理PersistenceUnit和PersistenceContext注解,注入相应的JPA资源,EntityManagerFactory
//, EntityManager,自动注入任何SpringBean中有此注解的字段,方法
if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition();
try {
def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
AnnotationConfigUtils.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
}
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//注册@EventListener为单独的实例
if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
//支持常规的注解实现
if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
}
return beanDefs;
}
再来看看这些注解解析处理器是怎么注册的,源码如下
private static BeanDefinitionHolder registerPostProcessor(
BeanDefinitionRegistry registry, RootBeanDefinition definition, String beanName) {
//指定bean的角色
//ROLE_APPLICATION=0,Bean是应用的主要的一部分,通常对应用户自定义的Bean。
//ROLE_SUPPORT=1,Bean是一些比较大的配置支持部分,通常是外部的ComponentDefinition。
//ROLE_INFRASTRUCTURE=2,表明提供完全的后台角色,且与最终用户无关,注册完全属于
//ComponetDefinitionn内部工作的Bean时使用
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//注册,bean名称和bean定义
registry.registerBeanDefinition(beanName, definition);
//返回Bean定义holder对象
return new BeanDefinitionHolder(definition, beanName);
}
进一步注册,源码如下。
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
//验证
if (beanDefinition instanceof AbstractBeanDefinition) {
try {
((AbstractBeanDefinition) beanDefinition).validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Validation of bean definition failed", ex);
}
}
//
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
//如果已经包含名称为BeanName的Bean定义
if (existingDefinition != null) {
//如果不允许覆盖,抛出异常
if (!isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
}
//如果允许覆盖,且存在的角色小于目前bean的角色
else if (existingDefinition.getRole() < beanDefinition.getRole()) {
// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
if (logger.isInfoEnabled()) {
logger.info("Overriding user-defined bean definition for bean '" + beanName +
"' with a framework-generated bean definition: replacing [" +
existingDefinition + "] with [" + beanDefinition + "]");
}
}
//如果允许覆盖,且角色存在的大于等于目前Bean角色,且目前Bean定义不等于存在的Bean定义
else if (!beanDefinition.equals(existingDefinition)) {
if (logger.isDebugEnabled()) {
logger.debug("Overriding bean definition for bean '" + beanName +
"' with a different definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
// 覆盖
else {
if (logger.isTraceEnabled()) {
logger.trace("Overriding bean definition for bean '" + beanName +
"' with an equivalent definition: replacing [" + existingDefinition +
"] with [" + beanDefinition + "]");
}
}
this.beanDefinitionMap.put(beanName, beanDefinition);
}
// 原来的BeanDefinition等于null,覆盖
else {
//开始创建Bean了
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
this.beanDefinitionMap.put(beanName, beanDefinition);
List updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
removeManualSingletonName(beanName);
}
}
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
removeManualSingletonName(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (existingDefinition != null || containsSingleton(beanName)) {
resetBeanDefinition(beanName);
}
}
接下来看AnnotationConfigApplicationContext的另一个属性ClassPathBeanDefinitionScanner,在AnnotationConfigApplicationContext实例化时,都做了那些事情。
/**
* registry,AnnotationConfigApplicationContext实现了DefinitionRegistry接口,即自己本身
* userDefaultFilters,true
* environment,AnnotationConfigApplicationContext实现了EnvironmentCapable,即自己的属性
* resourceLoader,AnnotationConfigApplicationContext实现了ResourceLoader,即自己本身
*/
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;
if (useDefaultFilters) {
//注册默认的过滤器,包括@Componet注解,和所有具有@Componet注解的注解
registerDefaultFilters();
}
setEnvironment(environment);
setResourceLoader(resourceLoader);
}
注册默认过滤器,扫描过滤注解类,在扫描注解配置的时候会用到。源码如下:
/**
* 注册@Component默认的filter,将会注册所有的注解包含@Componet注解的注解,包括@Repository,
* @Service,@Controller,也支持JavaEE6的ManageBean,JSR-330的@Named注解,如果可用的话
*/
@SuppressWarnings("unchecked")
protected void registerDefaultFilters() {
//添加@Componet注解的filter
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
//添加JSR-250注解filter
this.includeFilters.add(new AnnotationTypeFilter(
((Class extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try {
//添加JSR-330注解filter
this.includeFilters.add(new AnnotationTypeFilter(
((Class extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
在设置ResourceLoader的过程中,还初始化了ResourcePatternResolver,源码如下:
@Override
public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {
// resourceLoader本身实现了ResourcePatternResolver,就使用本身,否则创建一个默认的
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
// 元数据读取工厂
this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
// 提供对候选的访问,如获取com.weihao包下面的所有的@Component候选,
// candidates = index.getCandidateTypes("com.weihao",
// "org.springframework.stereotype.Component")
this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(this.resourcePatternResolver.getClassLoader());
}
在AnnotationConfigApplicationContext初始化之后,就是指定包名称,调用scan扫描配置的过程了,我们接下来看一下scan都做了哪些事情。调用scan的源码如下。
public void scan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
//实际上是调用了ClassPathBeanDefinitionScanner来扫描
this.scanner.scan(basePackages);
}
根据源码可以看到,实际上调用了ClassPathBeanDefinitionScanner来扫描Bean定义的。
public int scan(String... basePackages) {
//获取scan之前的BeanDefinition数量
int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
//扫描包
doScan(basePackages);
// 注册注解配置处理器,如果必要的话
// Register annotation config processors, if necessary.
if (this.includeAnnotationConfig) {
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
//返回新扫描的BeanDefinition
return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
}
根据上面的代码,具体的逻辑在doScan中,源码如下。
protected Set doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
// BeanDefinition Holder集合
Set beanDefinitions = new LinkedHashSet<>();
// 遍历包
for (String basePackage : basePackages) {
//查找候选的BeanDefinition集合
Set candidates = findCandidateComponents(basePackage);
//遍历候选BeanDefinition集合
for (BeanDefinition candidate : candidates) {
// 获取scop源数据
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
// 设置更多的配置,一般是Bean的默认配置,lazyInit,initMethodName,destoryMethodName等
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
// 设置通用的注解配置,lazyInit,primary等
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
// 检查bean定义,bean名称,查看是否需要注册或者和存在的bean定义冲突
if (checkCandidate(beanName, candidate)) {
//创建Bean定义 Holder
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
// 处理scope,主要是target class proxy
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
// 添加到BeanDefinitionHolder
beanDefinitions.add(definitionHolder);
//注册
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
//返回BeanDefinition Holder集合
return beanDefinitions;
}
根据上面的代码,可以看到扫描到的BeanDefinition已经被注册到了Registry,看一下Spring如何查找候选的BeanDefinition集合,findCandidateComponents方法源码如下:
/**
* 在classpath扫描候选的componets
* @param basePackage 检查注解类的包
* @return 返回自动检查的相应的BeanDefinition集合
*/
public Set findCandidateComponents(String basePackage) {
if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
//使用索引,在索引上添加查询 类和注解的相关信息
return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
}
else {
//扫描
return scanCandidateComponents(basePackage);
}
}
查看具体的扫描,scanCandidateComponents,源码如下:
private Set scanCandidateComponents(String basePackage) {
Set candidates = new LinkedHashSet<>();
try {
//包路径转为资源路径
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + '/' + this.resourcePattern;
//获取资源
Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
boolean traceEnabled = logger.isTraceEnabled();
boolean debugEnabled = logger.isDebugEnabled();
for (Resource resource : resources) {
if (traceEnabled) {
logger.trace("Scanning " + resource);
}
if (resource.isReadable()) {
try {
//获取元数据
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
if (isCandidateComponent(metadataReader)) {
//创建BeanDefinition
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setResource(resource);
sbd.setSource(resource);
if (isCandidateComponent(sbd)) {
if (debugEnabled) {
logger.debug("Identified candidate component class: " + resource);
}
candidates.add(sbd);
}
else {
if (debugEnabled) {
logger.debug("Ignored because not a concrete top-level class: " + resource);
}
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not matching any filter: " + resource);
}
}
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to read candidate component class: " + resource, ex);
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not readable: " + resource);
}
}
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
}
return candidates;
}
其中扫描BeanDefinition有很多细节,如果仔细研究,也有非常大的一部分。这里就不在赘述。
扫描完成后,接下来将扫描到的BeanDefinition注册到Registry,源码和初始化AnnotationConfigApplicationContext时的注册相同,如下。这里就不在贴源码了。
所以,scan的过程结束后,BeanDefinition也已经注册到了Registry。
在BeanDefinition被注册到Registry后,接下来是refresh的过程,加载,刷新配置,刷新ApplicationContext。接下来通过读源码看一下,applicationContext在refresh过程中都做了哪些事情。refresh源码如下
@Override
public 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.准备BeanFactory
prepareBeanFactory(beanFactory);
try {
// 允许 子类的BeanFactory 完成执行方法
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// 执行作为Bean注册在Context的 完成执行方法
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// 拦截Bean创建,注册Bean执行器
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// 初始化context的消息源
// Initialize message source for this context.
initMessageSource();
// 初始化context的事件广播
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// 在特殊的context子类中初始化其他特殊的Bean
// Initialize other special beans in specific context subclasses.
onRefresh();
// 检查监听器Bean并注册
// Check for listener beans and register them.
registerListeners();
// 实例化所有剩余(非lazy-init)单例
// 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 {
// 释放缓存,重置Spring中常见缓存,因为不再需要metadata元数据。
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
根据refresh的每一步,进一步了解refresh的过程,到底做了什么
prepareRefresh,准备刷新,源码如下。
protected void prepareRefresh() {
// 设置启动事件,close状态设置为false,active状态设置为true
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true);
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
// 初始化context环境中的占位符属性源
// Initialize any placeholder property sources in the context environment.
initPropertySources();
// 校验被标记为需要的所有属性都可以解析,查看
// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
getEnvironment().validateRequiredProperties();
// 保存预刷新应用监听器
// Store pre-refresh ApplicationListeners...
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
// 重置本地监听器 为 预刷新状态
// Reset local application listeners to pre-refresh state.
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// 允许保存早期的,当广播可以时被发布出来应用事件
// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
this.earlyApplicationEvents = new LinkedHashSet<>();
}
准备刷新的过程中,又包括多步操作,初始化占位符属性元,校验需要的属性,保存预刷新监听器,初始化早期的应用事件集合等操作,细节不在一个一个展开分析了。
准备刷新后,接下来是通知子类刷新内部的BeanFactory。
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
// 刷新BeanFactory
refreshBeanFactory();
// 返回BeanFactory
return getBeanFactory();
}
如何刷新呢,主要是设置刷新状态并返回AnnotationConfigApplicationContext中的属性DefaultListableBeanFactory的实例。
@Override
protected final void refreshBeanFactory() throws IllegalStateException {
if (!this.refreshed.compareAndSet(false, true)) {
throw new IllegalStateException(
"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
}
this.beanFactory.setSerializationId(getId());
}
准备BeanFactory,prepareBeanFactory
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
//通知内部BeanFactory,DefaultListableBeanFactory,使用context的类加载器
// Tell the internal bean factory to use the context's class loader etc.
beanFactory.setBeanClassLoader(getClassLoader());
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// 使用context回调,配置BeanFactory
// Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// BeanFactory接口未在普通工厂中注册为可解析类型,消息源作为Bean注册并自动装配
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// 注册早期 post-processor 以检测内部Bean作为应用监听器
// Register early post-processor for detecting inner beans as ApplicationListeners.
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// 检测LoadTimeWeaver 如果发现准备编制
// Detect a LoadTimeWeaver and prepare for weaving, if found.
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// 注册默认环境Bean
// Register default environment beans.
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
获取Bean,即applicationContext.getBean()方法。源码如下:
protected T doGetBean(final String name, @Nullable final Class requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
//转变Bean名称
final String beanName = transformedBeanName(name);
Object bean;
// 检查单例缓存,以是有手动注册单例
// Eagerly check singleton cache for manually registered singletons.
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 + "'");
}
}
// 获取Bean
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// 如果已经创建了Bean,失败
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// 检查是不是Bean定义在factory中存在
// 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 {
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
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);
}
}
}
// Create bean instance.
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
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);
}
else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
else {
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, () -> {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
});
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName,
"Scope '" + scopeName + "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex);
}
}
}
catch (BeansException ex) {
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
}
至此,一个ApplicationContext就启动了起来。并且可以根据应用上下文ApplicationContext获取Bean。