手撕源码之SpringApplication.java【Spring Boot 2.4.4】

1382行,真是又长又臭。捏着鼻子,干起来。

0~86行,版权、包名和import,略过。

87~161行,类注释信息,还是要看一下。

Class that can be used to bootstrap and launch a Spring application from a Java main method. 
该类可以用在main方法中使用,用来启动一个Spring应用程序。
By default class will perform the following steps to bootstrap your application:
默认情况下,该类以下面的步骤启动程序:
Create an appropriate ApplicationContext instance (depending on your classpath)
根据类路径创建一个ApplicationContext实例
Register a CommandLinePropertySource to expose command line arguments as Spring properties
注册一个CommandLinePropertySource来将命令行参数作为Spring属性
Refresh the application context, loading all singleton beans
刷新程序上下文,加载所有的单例bean
Trigger any CommandLineRunner beans
触发全部CommandLineRunner bean

In most circumstances the static run(Class, String[]) method can be called directly from your main method to bootstrap your application:
在很多情况下,在main方法中直接调用静态方法run(Class, String[])来启动程序,代码如下:
   @Configuration
   @EnableAutoConfiguration
   public class MyApplication  {
  
     // ... Bean definitions
  
     public static void main(String[] args) {
       SpringApplication.run(MyApplication.class, args);
     }
   }
   
For more advanced configuration a SpringApplication instance can be created and customized before being run:
也可以调用实例方法run(String... args)来启动程序,这样需要先创建SpringApplication实例,但好处是这样做可以自定义参数配置,代码如下:
   public static void main(String[] args) {
     SpringApplication application = new SpringApplication(MyApplication.class);
     // ... customize application settings here
     application.run(args)
   }
   
SpringApplications can read beans from a variety of different sources. 
SpringApplication可以从不同类型的sources中读取bean信息。
It is generally recommended that a single @Configuration class is used to bootstrap your application, however, you may also set sources from:
推荐这么做,main方法所在类用@Configuration修饰,但是并不影响我们设置sources的方法:
The fully qualified class name to be loaded by AnnotatedBeanDefinitionReader
由AnnotatedBeanDefinitionReader加载全完限定类名
The location of an XML resource to be loaded by XmlBeanDefinitionReader, or a groovy script to be loaded by GroovyBeanDefinitionReader
XmlBeanDefinitionReader加载XML resource,或者是GroovyBeanDefinitionReader加载groovy脚本
The name of a package to be scanned by ClassPathBeanDefinitionScanner
ClassPathBeanDefinitionScanner来根据报名扫描

Configuration properties are also bound to the SpringApplication. 
配置属性也绑定到SpringApplication
This makes it possible to set SpringApplication properties dynamically, like additional sources ("spring.main.sources" - a CSV list) the flag to indicate a web environment ("spring.main.web-application-type=none") or the flag to switch off the banner ("spring.main.banner-mode=off").
这么做使得能够动态设置SpringApplication的属性。(举了三个设置属性的例子,没翻译)

Since:
1.0.0
See Also:
run(Class, String[]), run(Class[], String[]), SpringApplication(Class...)
Author:
Phillip Webb, Dave Syer, Andy Wilkinson, Christian Dupuis, Stephane Nicoll, Jeremy Rickard, Craig Burke, Michael Simons, Madhura Bhave, Brian Clozel, Ethan Rubinson
(都是一些与代码逻辑无关的信息。大概是
1.0.0版本就有这个类了
这段注解中提的的三个方法,这三个方法的使用去看这三个方法各自的注释,在IDE中是有提示窗的
作者就11个人,真是的!!难怪又臭又长..人家都是大佬啦,难怪自己看起来这么吃力
)

总结一下这段到底说了什么:

1. 这个用来启动Spring容器,然后这个类启动做了哪些事儿;

2. 用run方法启动,这个run方法有两个重载,一个静态方法一个实例方法,各自的优势;

3. 这个类支持从不同source读取bean的配置;

4. 通过此类能为Spring配置属性参数。(3和4就是说,通过这个类能方便的为Spring容器配置bean或者参数)。

162行,类名。这个类简直太好了,虽然长,但就是一个单独的类,没继承谁也没注解修饰。所以只是长,用线性思维分析就可以。相比,屏目前的你也松了口气吧。相信我们一定能看完。

public class SpringApplication {

163-204行,这里定义了6个String类型的常量,和一个日志对象。为了后面代码读起来比较通顺,对这些常量要有点印象。具体如下:


	/**
	 * The class name of application context that will be used by default for non-web
	 * environments.
       默认情况下,非web环境的application context使用这里定义的类(用类名来指定的)。
	 * @deprecated since 2.4.0 in favour of using a {@link ApplicationContextFactory}
       从2.4.0版本开始用ApplicationContextFactory代替。
	 */
	@Deprecated
	public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context."
			+ "annotation.AnnotationConfigApplicationContext";

	/**
	 * The class name of application context that will be used by default for web
	 * environments.
       默认情况下,web环境使用这里指定的类作为application context。
	 * @deprecated since 2.4.0 in favour of using an {@link ApplicationContextFactory}
       从2.4.0版本开始用ApplicationContextFactory代替。(反复提到类ApplicationContextFactory,暂且不想去看这个类,等用到再过去)
	 */
	@Deprecated
	public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."
			+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";

	/**
	 * The class name of application context that will be used by default for reactive web
     (和上面的一样)默认对于响应式web环境使用这个常量指定的类作为application context。
	 * environments.
	 * @deprecated since 2.4.0 in favour of using an {@link ApplicationContextFactory}
	 */
	@Deprecated
	public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
			+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";

	/**
	 * Default banner location.
       默认banner的位置。(就是Spring Boot程序已启动会在控制台打印Spring的标识,真的够无聊的,代码本来就够多了,还弄这些有的没的,徒给读源码的人增加阅读量,你们这11个作者是想考我们阅读理解吗?)
	 */
	public static final String BANNER_LOCATION_PROPERTY_VALUE = SpringApplicationBannerPrinter.DEFAULT_BANNER_LOCATION;

	/**
	 * Banner location property key.
       banner位置的属性键,就是在配置文件中的key是什么,这里还指向了另一个常量。(我就说嘛,增加一个banner的打印,增加了不止一个类,何必呢。何必呢?(何必要在一起,让我没勇气,让我独自在这寒冷的夜里~何必要在一起~~~~~~~~~~~~))
	 */
	public static final String BANNER_LOCATION_PROPERTY = SpringApplicationBannerPrinter.BANNER_LOCATION_PROPERTY;

	private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
    这行代码没给注解,但看常量名,应该是指定读取系统系统属性的key,网上搜了一下,是说Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标。在430行configureHeadlessProperty这个方法中用到了。没太懂,先跳过吧。有点印象,等用到这个方法再细看。

	private static final Log logger = LogFactory.getLog(SpringApplication.class);
    这个就是个日志输出的对象了,太熟悉了~~

2021.4.6,先这样吧,明天继续。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,你遇到的问题是在构建Spring Boot项目时,无法找到项目'org.springframework.boot:spring-boot-starter-parent:2.2.11.RELEASE'。根据引用\[3\]的解决办法,你可以在对应的pom.xml文件中添加版本号绑定来解决这个问题。具体来说,你需要在父项目依赖处添加版本号绑定,如下所示: ```xml <!-- 父项目依赖处得到版本号 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.4</version> <!-- 这里是你需要绑定的版本号 --> <relativePath/> <!-- lookup parent from repository --> </parent> ``` 然后,在对应插件处绑定版本号,如下所示: ```xml <!-- 在对应插件处绑定版本号 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- 这里是你需要绑定的版本号 --> <version>2.4.4</version> </plugin> </plugins> </build> ``` 通过添加版本号绑定,你应该能够解决找不到项目'org.springframework.boot:spring-boot-starter-parent:2.2.11.RELEASE'的问题。 #### 引用[.reference_title] - *1* [Project ‘org.springframework.boot:spring-boot-starter-parent:2.6.1‘ not found](https://blog.csdn.net/qq_46091686/article/details/122063629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【报错】IDEA构建SpringBoot时,MVN报错未找到插件 ‘org.springframework.bootspring-boot-maven-plugin](https://blog.csdn.net/m0_67392273/article/details/124100268)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

horo99

求个赞啦

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

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

打赏作者

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

抵扣说明:

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

余额充值