AbstractApplicationContext的refresh()方法简要说明

本文详细解析了Spring容器启动过程中的关键步骤,包括BeanFactory的准备、Bean的实例化及初始化等,为读者深入理解Spring框架提供了清晰的指导。
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;
			}
		}
	}

 

ception: Failed to parse configuration class [com.hinmsbackground.NMSmaintenance.NMSmaintenanceApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.class] cannot be opened because it does not exist 2025-08-21 16:45:08.391 ERROR [] [main] org.springframework.boot.SpringApplication - Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.hinmsbackground.NMSmaintenance.NMSmaintenanceApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.class] cannot be opened because it does not exist at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:319) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) at com.hinmsbackground.NMSmaintenance.NMSmaintenanceApplication.main(NMSmaintenanceApplication.java:32) Caused by: java.io.FileNotFoundException: class path resource [com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.class] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值