• Spring-boot运行原理(主启动类)


    Spring-boot运行原理(主启动类)


    默认的主启动类

    //@SpringBootApplication 来标注一个主程序类 , 说明这是一个Spring Boot应用 @SpringBootApplication
    public class SpringbootApplication {
    	public static void main(String[] args) {
    		 //以为是启动了一个方法,实际启动了一个服务
    		SpringApplication.run(SpringbootApplication.class, args); 
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    我们来了解一个这些注解都干了什么

    @SpringBootApplication

    作用:标注在某个类上说明这个类是SpringBoot的主配置类 ,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

    进入这个注解:可以看到上面还有很多其他注解!

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
    ), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
    )})
    public @interface SpringBootApplication { // ......
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    @ComponentScan

    这个注解在Spring中很重要 ,它对应XML配置中的元素。
    作用:自动扫描并加载符合条件的组件或者bean , 将这个bean定义加载到IOC容器中

    @SpringBootConfiguration

    作用:SpringBoot的配置类 ,标注在某个类上 , 表示这是一个SpringBoot的配置类;
    我们继续进去这个注解查看

    // 点进去得到下面的 @Component
    @Configuration
    public @interface SpringBootConfiguration {}
    @Component
    public @interface Configuration {}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这里的 @Configuration,说明这是一个配置类 ,配置类就是对应Spring的xml 配置文件; 里面的 @Component 这就说明,启动类本身也是Spring中的一个组件而已,负责启动应用! 我们回到 SpringBootApplication 注解中继续看。

    @EnableAutoConfiguration

    @EnableAutoConfiguration :开启自动配置功能
    以前我们需要自己配置的东西,而现在SpringBoot可以自动帮我们配置 ; @EnableAutoConfiguration
    告诉SpringBoot开启自动配置功能,这样自动配置才能生效;

    点进注解接续查看:
    @AutoConfigurationPackage : 自动配置包

    @Import({Registrar.class})
    public @interface AutoConfigurationPackage {
     }
    
    • 1
    • 2
    • 3

    @import :Spring底层注解@import , 给容器中导入一个组件

  • 相关阅读:
    关于mysql存储过程中的拼接Sql语句写法
    【华为OD机试python】分割数组的最大差值【2023 B卷|100分】
    HTML常用标签
    帝国CMS后台登录显示空白解决方法汇总
    2023年9月随笔之摩托车驾考
    C# 连接Linux中的redis
    面试系列-Springboot 自定义starter使用详解
    Python机器学习实战-特征重要性分析方法(8):方差分析ANOVA(附源码和实现效果)
    19-springcloud(下)
    17.Table Api基础概念讲解
  • 原文地址:https://blog.csdn.net/qq_41359998/article/details/123085351