首页 > 其他分享 >Spring Boot 3系列之-启动类详解

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

时间:2023-11-05 23:00:51浏览次数:33  
标签:lang java Spring Boot 详解 注解 import annotation

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启动类。

标签:lang,java,Spring,Boot,详解,注解,import,annotation
From: https://blog.51cto.com/xiuji/8196692

相关文章

  • vue 网络图片访问不到,403的解决办法(详解)
    index.html中添加 <metaname="referrer"content="no-referrer"/>原因:防盗链的机制:通过页面的referrer信息,判断访问者来源,是否本站点,然后对图片等请求作出相应no-referrer:1、整个Referer首部包含了当前请求页面的来源页面的地址,即表示当前页面是通过此来源页面里的链接进......
  • Maven详解
    一.前言   以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar包的。最近一段时间在项目过程中使用Maven,用Maven构建的web项目,其项目结构只停留在了解阶段,没有深入的使用与理解,刚好最近看了一篇关于Maven的详解;就开始深入学习一下Maven的具体应用。......
  • 【MySQL】MVCC机制、ReadView数据结构、匹配规则详解
    (目录)MySQLMVCC机制1.隔离级别在MySQLInnoDB存储引擎下,RC、RR基于MVCC(多版本并发控制)进行并发事务控制MVCC是**基于”数据版本”**对并发事务进行访问2.场景分析UNDO_LOG不是会被删除吗?中间数据万一被删了版本链不就断了?UNDO_LOG版本链不是立即删除,MySQL确保版......
  • spring SSM搭建demo
    一、创建maven项目二、导入相关坐标在pom.xml中加入<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.10.RELEAS......
  • Spring 冷知识:一个提前 AOP 的机会
    今天再来聊一个Spring中的冷门知识:Bean的处理不走正常流程,而是提前进行AOP。本文算是前面文章(SpringBean名称暗藏玄机,这样取名就不会被代理)内容的一个补充,如果还没阅读前文,建议先阅读,这样有利于更好的理解本文。1.Bean创建流程前面文章中,松哥和大家梳理了,在Bean创建的过......
  • 如何让 Bean 深度感知 Spring 容器
    Spring有一个特点,就是创建出来的Bean对容器是无感的,一个Bean是怎么样被容器从一个Class整成一个Bean的,对于Bean本身来说是不知道的,当然也不需要知道,也就是Bean对容器的存在是无感的。但是有时候我们可能会遇到一些场景,这些场景让我们去感知容器的存在,松哥举几个例子:Sp......
  • Linux脚本:批量启动docker容器、批量启动springboot、批量启动Vuejs
    批量启动springboot#!/bin/bash#检查容器是否已经启动check_container(){sudodockerps|grep"$1">/dev/nullif[$?-ne0];thenecho"$1containerisnotrunning.Starting$1..."sudodockerstart"$1"elseecho......
  • MySQL教程:缓冲池Buffer Pool详解
    用于存储数据库的数据页和索引页查看缓冲池的大小showvariableslike'innodb_buffer_pool_size';查看缓冲池的使用情况showengineinnodbstatus;分析缓冲池命中率showstatuslike'innodb_buffer_pool_hit_rate';缓冲池满了LRU内存淘汰策略最近最少使用写回策略......
  • 11、SpringMVC之文件下载和上传
    创建名为spring_mvc_file的新module,过程参考9.1节和9.5节11.1、文件下载11.1.1、创建图片目录并放置图片11.1.2、页面请求示例<ath:href="@{/test/down}">下载图片</a>11.1.3、控制器方法示例packageonline.liaojy.controller;importorg.springframework.http.Ht......
  • [NewStarCTF WEEK5] pwn-planet 详解
    这道题目更多是考pwner的逆向功底(虽然程序逻辑也不是非常复杂=_=)老规矩,先checksec查看程序保护全开看一下main函数__int64__fastcallmain(inta1,char**a2,char**a3){unsignedintv4;//eaxchars1[88];//[rsp+20h][rbp-60h]BYREFunsigned__int64v6;......