45、Spring Boot 详细讲义(二)

发布于:2025-04-14 ⋅ 阅读:(27) ⋅ 点赞:(0)

三、SpringBoot 结构

1.SpringBoot 工作原理

Spring boot应用程序采用各种Starters启动器,入口类是包含@SpringBootApplication注解和main方法的类,然后使用@ComponentScan注解自动扫描项目中的所有组件,并且Spring Boot会根据@EnableAutoConfiguration注解将项目中的依赖项自动配置到应用程序中.例如,如果MySQL数据库在类路径上,但尚未配置任何数据库连接,则Spring Boot会自动配置内存数据库.

2. SpringBoot入口类

Spring Boot应用程序的入口点是包含@SpringBootApplication注解的类,该类中包含运行Spring Boot应用程序的main方法.默认一般是一个* application 结尾。

@SpringBootApplication注解包括自动配置,组件扫描和Spring Boot配置.
如果将@SpringBootApplication注解添加到类中,则无需添加@EnableAutoConfiguration,@ComponentScan和@SpringBootConfiguration注解.@SpringBootApplication注解包括其他所有的注解.

3.常见注解

3.1 @SpringBootApplication

@SpringBootApplication注解是Spring Boot的核心注解,用于标注程序是一个springboot 程序,它是一个组合注解,由多个注解组合而成
@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.2 @SpringBootConfiguration注解

@SpringBootConfiguration注解可以用Java代码的形式实现spring中xml配置文件配置的效果。也就是用来加载配置文件。
@Target({
   ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
   
}

3.3 @EnableAutoConfiguration注解

启用自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项,这也是 springboot 的核心注解之一,我们只需要将项目需要的依赖包导入进来,它会自动帮我们配置这个依赖需要的基本配置
比如我们的项目引入了spring-boot-starter-web依赖,springboot 会自动帮我们配置 tomcat 和 springmvc

3.4 @ComponentScan 注解

默认扫描@SpringBootApplication类所在包的同级目录以及它的子目录。用来扫描组件和自动装配。

springboot 支持的自动配置可以查看到

在spring-boot-autoconfigure包中包含了支持的自动装配依赖,截图只是其中一部分,我们开发中常见的依赖都做了基本配置

在这里插入图片描述

在这里插入图片描述

4. Springboot 全局配置文件

4.1.格式

​ SpringBoot使用的全局配置文件有两种格式:

​ • application.properties

​ • application.yml

​ 虽然后缀不一样,但是项目中有一个就可以了,yml格式比properties格式的简洁一些。

​ 这个文件的位置在配置文件放在src/main/resources目录或者类路径/config下。

  properties 和 .yml,它们的区别主要是书写格式不同。
    1).properties
      server.port=8088
      server.servlet.context-path=/testboot
    2).yml
    server:
      port: 8088
      servlet:
        context-path: /testboot
    另外yml 格式不支持 @PropertySource 注解导入配置 
    :和值之间有一个空格

4.2.作用

​ 全局配置文件的作用是:可以对一些默认配置值进行修改。

5.starter 启动器

springboot 提供了很多 starter, 可以帮我们快速构建相应的开发环境,导入相关的依赖,并设置相关配置

Name Description Pom
spring-boot-starter Core starter, including auto-configuration support, logging and YAML 核心启动器,包括自动配置,日志以及 YAML 支持 Pom
spring-boot-starter-activemq Starter for JMS messaging using Apache ActiveMQ 对Apache ActiveMQ提供支持 Pom
spring-boot-starter-amqp Starter for using Spring AMQP and Rabbit MQ 对Spring AMQP 和 Rabbit MQ支持 Pom
spring-boot-starter-aop Starter for aspect-oriented programming with Spring AOP and AspectJ 对 spring AOP和AspectJ提供支持 Pom
spring-boot-starter-artemis Starter for JMS messaging using Apache Artemis 对Apache Artemis提供支持 Pom
spring-boot-starter-batch Starter for using Spring Batch Pom
spring-boot-starter-cache Starter for using Spring Framework’s caching support Pom
spring-boot-starter-cloud-connectors Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku Pom
spring-boot-starter-data-cassandra Starter for using Cassandra distributed database and Spring Data Cassandra Pom
spring-boot-starter-data-cassandra-reactive Starter for using Cassandra distributed database and Spring Data Cassandra Reactive Pom
spring-boot-starter-data-couchbase Starter for using Couchbase document-oriented database and Spring Data Couchbase Pom
spring-boot-starter-data-couchbase-reactive Starter for using Couchbase document-o

网站公告

今日签到

点亮在社区的每一天
去签到