SpringBoot源码学习(一)启动

SpringBoot启动流程

框架的初始化

框架的启动

自动化装配

入口

SpringApplication.run(CoreFacadeApplication.class, args);

然后调用

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
   return new SpringApplication(primarySources).run(args);
}

初始化SpringApplication

	public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		//1.配置resourceLoader
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		//2.配置PrimarySources
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		//3.配置webApplicationType
		this.webApplicationType = org.springframework.boot.WebApplicationType.deduceFromClasspath();
		//4.配置ApplicationContextInitializer,初始化实例
		//从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		//5.从类路径下找到ETA-INF/spring.factories配置的所有ApplicationListener,到容器里
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		//6.配置mainApplicationClass,从多个配置类中找到有main方法的主配置类
		//在进行main方法的推断时,主要使用了堆栈信息一层层的判断,来获得main方法
		//基本流程就是创建一个运行时异常,然后获得堆栈数组,遍历StackTraceElement数组,判断方法名称是否为“mian”,
		// 如果过是则通过Class.forName()方法创建Class对象
		//这逻辑有点狠
		this.mainApplicationClass = deduceMainApplicationClass();
	}

 

框架启动

	public ConfigurableApplicationContext run(String... args) {
                //1.打印启动时间
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
                //2.设置java.awt.headless,Headless模式是系统的一种配置模式。在系统可能缺少显示设备、键盘或鼠标这些外设的情况下可以使用该模式。
		configureHeadlessProperty();
                //3.实例化SpringApplicationRunListener接口实现类//EventPublishingRunListener,设置启动类监听,
//用来在整个启动流程中接收不同执行点事件通知的监听者
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
                        //设置ApplicationArguments
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
                        //配置环境和相关属性
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
                        //打印banner
			Banner printedBanner = printBanner(environment);
                        //根据环境获取应用上下文类
			context = createApplicationContext();
                        //实例化异常处理器,        //org.springframework.boot.SpringBootExceptionReporter=\
//org.springframework.boot.diagnostics.FailureAnalyzers
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
                        //准备上下文
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
                        //刷新上下文
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
                        //执行EventPublishingRunListner中的started方法
			listeners.started(context);
                         //回调runners
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}

		try {
                        //发送ApplicationReadyEvent
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

 

### 解决 PP-OCRv4 出现的错误 当遇到 `WARNING: The pretrained params backbone.blocks2.0.dw_conv.lab.scale not in model` 这样的警告时,这通常意味着预训练模型中的某些参数未能匹配到当前配置下的模型结构中[^2]。 对于此问题的一个有效解决方案是采用特定配置文件来适配预训练权重。具体操作方法如下: 通过指定配置文件 `ch_PP-OCRv4_det_student.yml` 并利用已有的最佳精度预训练模型 (`best_accuracy`) 来启动训练过程可以绕过上述不兼容的问题。执行命令如下所示: ```bash python3 tools/train.py -c configs/det/ch_PP-OCRv4/ch_PP-OCRv4_det_student.yml ``` 该方案不仅解决了参数缺失带来的警告,还能够继续基于高质量的预训练成果进行微调,从而提升最终检测效果。 关于蒸馏的概念,在机器学习领域内指的是将大型复杂网络(teacher 模型)的知识迁移到小型简单网络(student 模型)。这里 student 和 teacher 的关系是指两个不同规模或架构的神经网络之间的指导与被指导的关系;其中 teacher 已经经过充分训练并具有良好的性能,而 student 则试图模仿前者的行为模式以达到相似的效果但保持更高效的计算特性。 至于提到的 `Traceback` 错误信息部分,由于未提供具体的跟踪堆栈详情,难以给出针对性建议。不过一般而言,这报错往往涉及代码逻辑错误或是环境配置不当等问题。为了更好地帮助定位和解决问题,推荐记录完整的异常日志,并仔细检查最近修改过的代码片段以及确认依赖库版本的一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值