`

AbstractApplicationContext的refresh()方法简要说明

阅读更多
public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			
			prepareRefresh();//准备启动spring容器,设置容器的启动日期和活动标志

			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();//获得容器ApplicationContext的子类BeanFactory。步骤如下:如果已经有了BeanFactory就销毁它里面的单例Bean并关闭这个BeanFactory。2.创建一个新的BeanFactory。3.对这个BeanFactory进行定制(customize),如allowBeanDefinitionOverriding,allowCircularReferences。4.转载BeanDefinitions(读取配置文件,将xml转换成对应得BeanDefinition)。5.检查是否同时启动了两个BeanFactory。

			prepareBeanFactory(beanFactory);//配置BeanFactory(就是将ApplicationContext的一些属性配置到BeanFactory上吧)。这里做了很多事情,可以看看其源码,比较重要的如设置classLoader;将BeanPostProcess注册到BeanFactory里。

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				
				invokeBeanFactoryPostProcessors(beanFactory);//触发BeanFactoryPostProcessors

				
				registerBeanPostProcessors(beanFactory);//触发BeanPostProcessors

				// 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();

				
				finishBeanFactoryInitialization(beanFactory);//初始化单例的Bean,并将其保存起来。

				// Last step: publish corresponding event.
				finishRefresh();
			}

			catch (BeansException ex) {
				// Destroy already created singletons to avoid dangling resources.
				beanFactory.destroySingletons();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}
		}
	}

 

分享到:
评论
2 楼 IXHONG 2017-11-01  
registerBeanPostProcessors(beanFactory);//只是注册,还未触发
1 楼 string2020 2016-04-19  
refresh到底是干啥的。
@SpringBootApplication
public class App5
{
public static void main(String[] args)
{
final SpringApplication application = new SpringApplication(App5.class);
final ConfigurableApplicationContext context = application.run(args);

new Thread(() -> {
try
{
TimeUnit.SECONDS.sleep(3);
} catch (Exception e)
{
e.printStackTrace();
}
context.refresh();
}).start();
}
}
Exception in thread "Thread-3" java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
这样为啥会报错

相关推荐

    testSpring

    2013-3-20 22:22:46 org.springframework.context.support.AbstractApplicationContext refresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e: display name [org....

    Spring启动流程.java

    AbstractApplicationContext.refresh(){ 1.为刷新准备此上下文 prepareRefresh(){ initPropertySources();//在上下文环境中初始化任何占位符属性源 getEnvironment().validateRequiredProperties();//验证所有...

    Spring高级之注解驱动开发视频教程

    n 源码分析-AbstractApplicationContext的refresh方法 n 源码分析-AbstractBeanFactory的doGetBean方法 l Spring Aop n 设计模式-代理模式 n 编程思想-AOP思想 n 基础应用-入门案例 n 基础应用-常用注解 n 高级应用-...

    struts2驱动包

    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381) at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext....

    sping applicationcontext中的一些例子demo

    sping applicationcontext中的一些例子demo 关于AbstractApplicationContext 以及它的几个常用子类的一些demo

    SpringCodeReadingNotes:Spring源码阅读笔记-spring源码阅读

    我们先创建一个类TestService,加上@Service和@Transactional注解,随便写一个保存数据的方法。 1.1.1注册BeanDefinition的阶段 xml扫描过后,发现把所有Bean定义打印出来,发现有一个bean叫做“ proxyTargetClass =...

    第14章:网络编程(day20).zip

    [[03 08:58:22,466 INFO ] org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:511) - Refreshing WebApplicationContext for namespace 'hessian-...

    SPRING API 2.0.CHM

    AbstractApplicationContext AbstractApplicationEventMulticaster AbstractAspectJAdvice AbstractAspectJAdvisorFactory AbstractAspectJAdvisorFactory.AspectJAnnotation AbstractAspectJAdvisorFactory....

Global site tag (gtag.js) - Google Analytics