当前位置: 首页 > news >正文

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);}
}

启动类上边的@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 {...
}

他主要组合了以下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;
}

@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;
}

注解@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 {};
}

注解@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 {...
}

总结

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

http://www.lryc.cn/news/221754.html

相关文章:

  • muduo源码剖析之Timer定时器
  • CocosCreator:背景滚动 、背景循环滚动
  • 中远麒麟堡垒机SQL注入漏洞复现
  • ActiveMq学习⑨__基于zookeeper和LevelDB搭建ActiveMQ集群
  • Ansible概述以及模块
  • Cannot run program “D:\c\IntelliJ IDEA 2021.1.3\jbr\bin\java.exe“
  • 案例-注册页面(css)
  • Ansible--playbook 剧本
  • Vue3.0路由拦截
  • EtherCAT转EtherNET/IP协议网关控制EtherCAT伺服驱动器的方法
  • 钉钉内嵌H5遇到的一些问题
  • LeetCode 热题100——链表专题(二)
  • 【Rust日报】2023-11-06 ESP上使用 Rust实现 SNTP协议
  • LibreOJ - 2874 历史研究 (回滚莫队)
  • 人工智能-卷积神经网络之多输入多输出通道
  • Open3D(C++) Umeyama算法求两个点云的变换矩阵
  • 【C++】从入门到精通第二弹——类的构造与析构函数
  • C#8.0本质论第十一章--异常处理
  • FPGA高端项目:图像缩放+GTP+UDP架构,高速接口以太网视频传输,提供2套工程源码加QT上位机源码和技术支持
  • ansible安装和常见模块
  • 【Python基础】 Python设计模式之单例模式介绍
  • 算法小白的心得笔记:关于Nan
  • Photoshop 2023 v24.7
  • 进程间通信(IPC)-管道、消息队列、信号量、共享存储、socket
  • 「Verilog学习笔记」使用generate…for语句简化代码
  • 互联网Java工程师面试题·Spring篇·第七弹
  • mysql驱动包引起的告警问题using SSL the verifyServerCertificate property is set to ‘false‘
  • draw.io与项目管理——如何利用流程图工具提高项目管理效率
  • LoRaWAN物联网架构
  • 数据结构(五):哈希表及面试常考的算法