• Spring Boot 3系列之-启动类详解


    Spring Boot是一个功能强大、灵活且易于使用的框架,它极大地简化了Spring应用程序的开发和部署流程,使得开发人员能够更专注于业务逻辑的实现。在我们的Spring Boot 3系列之一(初始化项目)文章中,我们使用了Spring官方网站生成的Spring Boot项目作为示例。在该项目中,我们可以找到一个名为XjdocApplication的启动类,它是Spring Boot应用程序的入口点。本文将详细解释这个启动类的作用和功能。

    springbootApplication.jpg

    Spring Boot启动类

    在Spring Boot中,启动类是整个应用程序的入口点。一般是放在项目的根路径下的(推荐放在项目的根路径下)。它是一个标注了 @SpringBootApplication 注解的 Java 类,必须包含一个标准的 main 方法,在main方法中添加SpringApplication.run()方法,用于启动 Spring Boot 应用程序。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    
    @SpringBootApplication
    public class XjdocApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(XjdocApplication.class, args);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    启动类上边的@SpringBootApplication是 注解应用启动的入口类,它自动开启了许多有用的特性,如自动配置、组件扫描、筹划配置类等,从而减少了开发人员的配置工作量。@SpringBootApplication是Spring Boot启动类上的核心注解,是一个组合注解,源码如下:

    @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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    他主要组合了以下3个注解:

    • @SpringBootConfiguration:表明被注解的类是一个配置类,用于定义应用程序的配置信息。
    • @EnableAutoConfiguration:开启自动配置功能。
    • @ComponentScan:启用组件扫描,使得Spring能够自动发现和装配一些组件

    关系图如下:

    springbootApplication.png

    注解@SpringBootConfiguration

    @SpringBootConfiguration 是Spring Boot提供的特定注解之一,它用于指示一个类是Spring Boot应用程序的配置类。该注解组合了 @Configuration 注解,它表示被标注的类可以作为配置类用于配置应用程序的上下文。与 @Configuration 注解类似,它通常与其他注解一起使用,用于定义和管理应用程序的配置信息。源码如下:

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.annotation.AliasFor;
    import org.springframework.stereotype.Indexed;
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration
    @Indexed
    public @interface SpringBootConfiguration {
        @AliasFor(
            annotation = Configuration.class
        )
        boolean proxyBeanMethods() default true;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    @Configuration 注解源码如下:

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.core.annotation.AliasFor;
    import org.springframework.stereotype.Component;
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Configuration {
        @AliasFor(
            annotation = Component.class
        )
        String value() default "";
    
        boolean proxyBeanMethods() default true;
    
        boolean enforceUniqueMethods() default true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    注解@EnableAutoConfiguration

    @EnableAutoConfiguration 是Spring Boot框架中的一个重要注解,它允许应用程序根据类路径中的依赖自动配置其组件。通过这个注解,Spring Boot可以根据应用程序的依赖关系自动配置各种组件。源码如下:

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.context.annotation.Import;
    
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import({AutoConfigurationImportSelector.class})
    public @interface EnableAutoConfiguration {
        String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    
        Class<?>[] exclude() default {};
    
        String[] excludeName() default {};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    注解@ComponentScan

    @ComponentScan 是Spring框架中的一个注解,用于指定Spring在哪些包中寻找组件。它会自动扫描并注册指定包中的所有带有 @Component 及其派生注解的类作为Spring的Bean。这样可以方便地将自定义的类纳入Spring的上下文中,使得它们可以被自动装配和使用。下面是对 @ComponentScan 注解的详细解释:

    • 指定扫描包: @ComponentScan 注解允许开发人员指定要扫描的包路径。在指定的包及其子包(默认当前目录及所有子目录)中,所有带有 @Component 及其派生注解的类都将被自动注册为Spring的Bean,这也是推荐把它放在项目根路径下的原因。

    • 自动注册组件: 通过 @ComponentScan 注解,开发人员可以方便地将自定义的类纳入Spring的上下文中,使得它们可以被自动装配和使用。这样可以提高开发效率,同时减少手动配置的工作量。

    • 定制扫描规则: @ComponentScan 注解还支持一些参数配置,允许开发人员定制扫描规则,如指定特定的注解、过滤规则等,以满足特定的需求。

    源码如下:

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Repeatable;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.beans.factory.support.BeanNameGenerator;
    import org.springframework.core.annotation.AliasFor;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE})
    @Documented
    @Repeatable(ComponentScans.class)
    public @interface ComponentScan {
      ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    总结

    Spring Boot启动类是构建Spring Boot应用程序的关键组成部分。它允许开发人员配置和管理应用程序的行为,同时简化了应用程序的配置和部署过程。通过深入了解Spring Boot启动类的功能和用法,开发人员可以更好地构建和管理复杂的Spring Boot应用程序。希望本文能够帮助您更好地理解和使用Spring Boot启动类。

  • 相关阅读:
    计算机毕业设计-微信小程序文学小说阅读销售系统
    WebKit Insie: Active 样式表
    哈希表及完整注释
    SonarQube的使用心得
    虚拟机的快照与克隆
    高新技术企业的认定申请材料有哪些?
    IOTE2024第二十一届(上海)国际物联网展览会4月24日-26日开幕
    JDK8-10-Optional(1)
    开发人员必备的万能工具箱:He3
    iOS动画相关
  • 原文地址:https://blog.csdn.net/weixin_44002151/article/details/134236464