springboot项结构分析

本文详细介绍了SpringBoot的结构,包括工作原理,入口类,常见注解,全局配置文件以及starter启动器。SpringBoot通过注解自动配置依赖,如在类路径上发现MySQL时会自动配置数据库。入口类通常包含@SpringBootApplication,它整合了自动配置,组件扫描和Spring Boot配置。全局配置文件可以是application.properties或application.yml,用于修改默认配置。Starter启动器帮助快速搭建开发环境并配置相关依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三. SpringBoot 结构

3.1.SpringBoot 工作原理

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

3.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 {
   
  //此处忽略接口内部内容
}
2 @SpringBootConfiguration注解
@SpringBootConfiguration注解可以用Java代码的形式实现spring中xml配置文件配置的效果。也就是用来加载配置文件。
@Target({
   ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
   
}
3 @EnableAutoConfiguration注解
启用自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项,这也是 springboot 的核心注解之一,我们只需要将项目需要的依赖包导入进来,它会自动帮我们配置这个依赖需要的基本配置
比如我们的项目引入了spring-boot-starter-web依赖,springboot 会自动帮我们配置 tomcat 和 springmvc
4 @ComponentScan 注解
默认扫描@SpringBootApplication类所在包的同级目录以及它的子目录。用来扫描组件和自动装配。

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

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

在这里插入图片描述

在这里插入图片描述

3.4 Springboot 全局配置文件

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 注解导入配置 
    :和值之间有一个空格
2.作用

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

3.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-oriented database and Spring Data Couchbase Reactive Pom
spring-boot-starter-data-elasticsearch Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch Pom
spring-boot-starter-data-jpa Starter for using Spring Data JPA with Hibernate Pom
spring-boot-starter-data-ldap Starter for using Spring Data LDAP Pom
spring-boot-starter-data-mongodb Starter for using MongoDB document-oriented database and Spring Data MongoDB Pom
spring-boot-starter-data-mongodb-reactive Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive Pom
spring-boot-starter-data-neo4j Starter for using Neo4j graph database and Spring Data Neo4j Pom
spring-boot-starter-data-redis Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client Pom
spring-boot-starter-data-redis-reactive Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client Pom
spring-boot-starter-data-rest Starter for exposing Spring Data repositories over REST using Spring Data REST Pom
spring-boot-starter-data-solr Starter for using the Apache Solr search platform with Spring Data Solr Pom
spring-boot-starter-freemarker Starter for building MVC web applications using FreeMarker views Pom
spring-boot-starter-groovy-templates Starter for building MVC web applications using Groovy Templates views Pom
spring-boot-starter-hateoas Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS Pom
spring-boot-starter-integration Starter for using Spring Integration Pom
spring-boot-starter-jdbc Starter for using JDBC with the Tomcat JDBC connection pool Pom
spring-boot-starter-jersey Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web Pom
spring-boot-starter-jooq Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc Pom
spring-boot-starter-json Starter for reading and writing json Pom
spring-boot-starter-jta-atomikos Starter for JTA transactions using Atomikos Pom
spring-boot-starter-jta-bitronix Starter for JTA transactions using Bitronix Pom
spring-boot-starter-jta-narayana Spring Boot Narayana JTA Starter Pom
spring-boot-starter-mail Starter for using Java Mail and Spring Framework’s email sending support Pom
spring-boot-starter-mustache Starter for building web applications using Mustache views Pom
spring-boot-starter-quartz Spring Boot Quartz Starter Pom
spring-boot-starter-security Starter for using Spring Security Pom
spring-boot-starter-test Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito Pom
spring-boot-starter-thymeleaf Starter for building MVC web applications using Thymeleaf views Pom
spring-boot-starter-validation Starter for using Java Bean Validation with Hibernate Validator Pom
spring-boot-starter-web Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container Pom
spring-boot-starter-web-services Starter for using Spring Web Services Pom
spring-boot-starter-webflux Starter for building WebFlux applications using Spring Framework’s Reactive Web support Pom
spring-boot-starter-websocket Starter for building WebSocket applications using Spring Framework’s WebSocket support Pom

附录 springboot 全局配置属性

# ===================================================================
# COMMON SPRING BOOT PROPERTIES
#
# This sample file is provided as a guideline. Do NOT copy it in its
# entirety to your own application.               ^^^
# ===================================================================


# ----------------------------------------
# CORE PROPERTIES
# ----------------------------------------

# BANNER
banner.charset=UTF-8 # Banner file encoding.
banner.location=classpath:banner.txt # Banner file location.
banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).
banner.image.width= # Width of the banner image in chars (default 76)
banner.image.height= # Height of the banner image in chars (default based on image height)
banner.image.margin= # Left hand image margin in chars (default 2)
banner.image.invert= # If images should be inverted for dark terminal themes (default false)

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值