@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

相关文章

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

SQL 注入攻击(SQL Injection)原理、利用方式与防御策略深度解析

《SQL注入攻击(SQLInjection)原理、利用方式与防御策略深度解析》本文将从SQL注入的基本原理、攻击方式、常见利用手法,到企业级防御方案进行全面讲解,以帮助开发者和安全人员更系统地理解... 目录一、前言二、SQL 注入攻击的基本概念三、SQL 注入常见类型分析1. 基于错误回显的注入(Erro

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关

Springboot主配置文件解析

《Springboot主配置文件解析》SpringBoot主配置文件application.yml支持多种核心值类型,包括字符串、数字、布尔值等,文章详细介绍了Profile环境配置和加载位置,本文... 目录Profile环境配置配置文件加载位置Springboot主配置文件 application.ym

Java中Redisson 的原理深度解析

《Java中Redisson的原理深度解析》Redisson是一个高性能的Redis客户端,它通过将Redis数据结构映射为Java对象和分布式对象,实现了在Java应用中方便地使用Redis,本文... 目录前言一、核心设计理念二、核心架构与通信层1. 基于 Netty 的异步非阻塞通信2. 编解码器三、

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

一文解析C#中的StringSplitOptions枚举

《一文解析C#中的StringSplitOptions枚举》StringSplitOptions是C#中的一个枚举类型,用于控制string.Split()方法分割字符串时的行为,核心作用是处理分割后... 目录C#的StringSplitOptions枚举1.StringSplitOptions枚举的常用

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4