@SpringBootApplication和SpringApplication.run源码解析:

2024-03-23 02:28

本文主要是介绍@SpringBootApplication和SpringApplication.run源码解析:,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 一、@SpringBootApplication和自动配置
    • 1、主要功能
    • 2、@SpringBootConfiguration
    • 3、@EnableAutoConfiguration
    • 4、@ComponentScan
    • 5、Spring boot自动配置
        • 5.1、@Configuration开启自动配置
        • 5.2、查看自动配置
        • 5.3、禁用自动配置
  • 二、run()


当前版本

        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>

一、@SpringBootApplication和自动配置

/*** Indicates a {@link Configuration configuration} class that declares one or more* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience* annotation that is equivalent to declaring {@code @Configuration},* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.** @author Phillip Webb* @author Stephane Nicoll* @author Andy Wilkinson* @since 1.2.0*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

1、主要功能

  • @EnableAutoConfiguration:启用Spring Boot的自动配置机制。
  • == @ComponentScan==:对应用程序所在的包启用 @Component 扫描(见最佳实践)。
  • @SpringBootConfiguration:允许在Context中注册额外的Bean或导入额外的配置类。这是Spring标准的 @Configuration 的替代方案,有助于在你的集成测试中检测配置。

这几个功能都可以一定程度的自定义 ,列如你不需要在你的应用程序中使用组件扫描或配置属性扫描。

@SpringBootConfiguration(proxyBeanMethods = false)就 不能自动检测到 @Component 和 @ConfigurationProperties(这两个是SpringBootConfiguration类下引用了这两个注解) 注解的类,而是明确导入用户定义的Bean

@SpringBootConfiguration(proxyBeanMethods = false)
@EnableAutoConfiguration
@Import({ SomeConfiguration.class, AnotherConfiguration.class })
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}}

2、@SpringBootConfiguration

//指示类提供Spring Boot application@Configuration。
//可以用作Spring的标准@Configuration注释的替代品,以便可以自动找到配置(例如在测试中)。
//应用程序应该只包含一个@SpringBootConfiguration,大多数惯用的SpringBoot应用程序将从@SpringBootApplication继承它。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration {

3、@EnableAutoConfiguration

启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。自动配置类通常基于类路径和您定义的bean来应用。例如,如果您的类路径上有tomcat-embedded.jar,那么您可能想要一个TomcatServlet WebServerFactory(除非您定义了自己的Servlet WebServerFactoryBean)。
当使用@SpringBootApplication时,上下文的自动配置会自动启用,因此添加此注释没有其他效果。
自动配置试图尽可能地智能化,并将随着您定义更多自己的配置而退出。您总是可以手动排除()任何您永远不想应用的配置(如果您没有访问权限,请使用excludeName())。您也可以通过spring.autoconfig.exclude属性排除它们。自动配置总是在用户定义的bean注册后应用

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

4、@ComponentScan

配置与@Configuration类一起使用的组件扫描指令。提供与Spring XML的<context:component scan>元素并行的支持。
可以指定basePackageClasses或basePackages(或其别名值)来定义要扫描的特定包。
如果未定义特定的包,则将从声明此注释的类的包进行扫描。
请注意,context:component-scan元素具有annotation-config属性;然而,这个注释没有。这是因为在几乎所有情况下,当使用@ComponentScan时,都会假设默认的注释配置处理(例如处理@Autowired和friends)。
此外,当使用AnnotationConfigApplicationContext时,注释配置处理器始终是注册的,这意味着在@ComponentScan级别禁用它们的任何尝试都将被忽略。

 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {

5、Spring boot自动配置

5.1、@Configuration开启自动配置

SpringBoot的自动装配机制会试图根据你所添加的依赖来自动配置你的Spring应用程序。比如你添加了数据库相关的依赖,Springboot就会自动配置数据库相关的内容,(你引入mysql依赖,但是你不去配置,就会触发数据库资源配置问题)

上面的@SpringBootApplication 或 @EnableAutoConfiguration 注解就是开启自动配置的注解,这两个在系统中只能出现一次
其他的自动配置使用 @Configuration注解注入自动配置

5.2、查看自动配置

如果要 的可以进入Debug模式查看
进入方法有

  • 命令行 启动时加入 --debug (jar 直接加就可以 ),maven命令行 mvn spring-boot:run --debug
  • properties添加属性 debug=true
  • 通过idea启动项上添加VM 配置 –Ddebug 新版idea只需要在 启动配置页面ALT+D就可触发在这里插入图片描述
5.3、禁用自动配置

第一个方式

@SpringBootApplication(exclude = { XXXXX.class })  

第二个方式

properties配置

spring.autoconfigure.exclude=XXXXXX

这两个配置其实是一个配置,只是写的地方不一样
在@SpringBootApplication的exclude属性

/*** Exclude specific auto-configuration classes such that they will never be applied.*  排除特定的自动配置类别,使其永不被应用。* @return the classes to exclude*/@AliasFor(annotation = EnableAutoConfiguration.class)Class<?>[] exclude() default {};/*** Exclude specific auto-configuration class names such that they will never be* applied.* @return the class names to exclude* @since 1.3.0*/@AliasFor(annotation = EnableAutoConfiguration.class)String[] excludeName() default {};

可以注意, SpringBootApplication里面有一个exclude和 excludeName
因为有的像禁用的包没有导入的包会找不到.class文件 这个上海就可以使用包的全路径名类似org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,这个样子

自动配置是非侵入性的。Spring有一套默认的配置,当你开始自定义配置时,你配置的bean会自动使用代替默认配置

TIPS Spring文档 中提到一个点,不要去使用自动配置类中的方法或者属性,包括嵌套的配置类。都不建议你去使用。

无论是单一原则,还是系统后期版本变动都不应该去使用这些。

二、run()

run方法是 SpringApplication类下的一个方法,是负责启动Spring Boot的入口

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

SpringApplication下供有三个run方法分别是

  • 静态方法, 使用默认设置从指定源运行SpringApplication。
	public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {return run(new Class<?>[] { primarySource }, args);}
  • 静态方法, 使用默认设置和用户提供的参数从指定的源运行SpringApplication。
	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {return new SpringApplication(primarySources).run(args);}
  • 运行Spring应用程序,创建并刷新一个新的ApplicationContext。
  public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();ConfigurableApplicationContext context = null;this.configureHeadlessProperty();SpringApplicationRunListeners listeners = this.getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);this.configureIgnoreBeanInfo(environment);Banner printedBanner = this.printBanner(environment);context = this.createApplicationContext();context.setApplicationStartup(this.applicationStartup);this.prepareContext(bootstrapContext, context,environment, listeners, applicationArguments, printedBanner);this.refreshContext(context);this.afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);}listeners.started(context);this.callRunners(context, applicationArguments);} catch (Throwable var10) {this.handleRunFailure(context, var10, listeners);throw new IllegalStateException(var10);}try {listeners.running(context);return context;} catch (Throwable var9) {this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);throw new IllegalStateException(var9);}}
  • 当你需要灵活配置或者又其他和Spring应用结构不同的情况下使用最后一个,

  • 前两个其实是一个,第一个方法也会走到第二个run方法,他这样就是为了同时满足多模块程序和单模块程序,

  • 下班了明天再写

参考文章

https://blog.csdn.net/u014131617/article/details/85335692

Spring中文文档

Spring文档

这篇关于@SpringBootApplication和SpringApplication.run源码解析:的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/836849

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s