SpringBoot启动的时候做了什么(1)

大家都知道SpringBoot是靠一个main()方法启动的,那么这个main()背后做了什么呢?
//本来是想找点八股文看看的,看到网上文章要么版本太低,要么还得关注公众号,要么太深入细节。太深入咱也看不懂啊,那咋整,自己扒拉代码自己看呗

sb版本:2.7.x
我直接从github上fork的源码来看的,如需自取:https://github.com/spring-projects/spring-boot
代码拉下来之后,项目结构是这样的
在这里插入图片描述
咱也不管啥结构了,直接找代码:SpringApplication
直接看run方法

//这里的primarySources就是我们的应用程序的主类,这里的args就是我们的应用程序的参数
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
	//这里可以看到SpringApplication的启动有两部分组成
	//1:实例化SpringApplication对象·
	//2:调用SpringApplication.run()方法进行启动
	return new SpringApplication(primarySources).run(args);
}

其实就是做了两步:

  1. 实例化SpringApplication对象
  2. run(args)

SpringApplication对象的实例化

new SpringApplication(primarySources)

创建对象的时候传了一个参数,这个primarySources其实就是从main()方法传过来的。
我们的main是这样的:

public static void main(String[] args) {
  	SpringApplication.run(DubboNacosProviderApplication.class, args);
}

run的第一个参数其实就是primarySources
接着SpringApplication的构造函数

public SpringApplication(Class<?>... primarySources) {
	this(null, primarySources);
}

继续点这个this

@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	this.resourceLoader = resourceLoader;
	Assert.notNull(primarySources, "PrimarySources must not be null");

	//将项目启动类(primarySources)存起来,用于启动项目时,获取项目启动类的类名
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));

	//获取下当前项目的类型,有三种类型:NONE、SERVLET、REACTIVE(spring5之后出现webflux交互式的框架)
	this.webApplicationType = WebApplicationType.deduceFromClasspath();

	//这是干嘛的?
	this.bootstrapRegistryInitializers = new ArrayList<>(
			getSpringFactoriesInstances(BootstrapRegistryInitializer.class));

	//设置初始化器(Initializer),最后会去调用这些初始化器的init方法
	//spring-boot-autoconfigure的spring.factories文件中的初始化器
	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

	//设置启动“main”的类,这个类是项目的主类,也就是项目的入口类
	this.mainApplicationClass = deduceMainApplicationClass();
}

其实就是主要做了以下几件事:

  1. 根据传进来的primarySources把入口类地址存一下,留着后边用。
  2. 判断下当前项目的类型,是mvc还是webflux。this.webApplicationType = WebApplicationType.deduceFromClasspath()这个方法就是根据当前classpath下是否有指定class去判断的,逻辑很简单。
  3. 设定初始化器和监听器

关于这个代码中的BootstrapRegistryInitializer ,是个接口,当前版本的spring.factories中也没找到相关默认实现,那肯定就是留给开发者自己去玩的,咱先不管是干啥的,接着往后边看。

ApplicationContextInitializer

# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
this.initializers = new ArrayList<>(initializers);

ApplicationListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener
this.= new ArrayList<>(listeners);

以上initializers listeners 后边用到的时候再讲

下一篇继续写run(args)的逻辑

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在Spring Boot项目启动时写入Redis,可以使用Spring Boot的启动事件来实现。下面是一个示例代码: 首先,创建一个监听器类,实现`ApplicationListener`接口,监听`ApplicationReadyEvent`事件,在项目启动完成后执行相应的操作: ```java import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisInitializer implements ApplicationListener<ApplicationReadyEvent> { private final RedisTemplate<String, String> redisTemplate; public RedisInitializer(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public void onApplicationEvent(ApplicationReadyEvent event) { // 在这里进行Redis的写入操作 redisTemplate.opsForValue().set("key", "value"); } } ``` 在上面的代码中,我们使用了`RedisTemplate`来进行Redis的操作。你可以根据实际需求修改`set`方法的参数来写入不同的数据。 然后,确保你已经正确配置了Redis相关的依赖和连接信息。在`application.properties`(或`application.yml`)文件中,添加Redis的连接配置信息,例如: ```properties spring.redis.host=localhost spring.redis.port=6379 ``` 最后,在你的Spring Boot项目中添加上述的监听器类,它会在项目启动完成后自动执行Redis写入操作。 这样,每次启动Spring Boot项目时,都会将数据写入Redis中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高级摸鱼工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值