Spring Boot(2.1.6.RELEASE)构建系统

本文探讨了在SpringBoot项目中使用Maven和Gradle作为构建系统的优劣,介绍了如何利用这些工具进行依赖管理,以及如何使用SpringBoot提供的各种starter来加速开发过程。

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

Build Systems选择建议

强烈建议选择支持依赖关系管理的构建系统,建议您选择Maven或Gradle。Spring Boot与可以与其他构建系统(例如Ant)一起工作,但它们并没有得到特别好的支持。

依赖关系管理

Spring Boot的每个版本都提供了默认支持的依赖列表(包含可以与Spring Boot一起使用的所有spring模块以及其他第三方库类库。)。并且不需要在pom.xml文件中提供这些默认依赖项的版本,因为Spring Boot会自动管理这些依赖。当您升级Spring Boot时,这些依赖的版本也会升级到与Spring Boot一致的版本。

TIPS:如果需要,可以指定版本号来并覆盖Spring Boot的默认版本。(每个版本的Spring Boot都与Spring Framework的基本版本相关联,不建议更改Spring Framework的版本好)

Maven

Maven 可通过继承spring-boot-starter-parent 来构建spring boot项目并获取一些默认值.

  • JDK默认版本为JDK1.8
  • 编码字符集为:UTF-8
  • 继承 spring-boot-dependencies 的依赖,用可于管理公共依赖的版本。此依赖管理允许在pom文件中省略这些依赖项的标记
  • 合理的资源过滤

TIPS:yml文件接受Spring样式的占位符(${…}),Maven过滤改为使用@…@占位符。(您可以通过设置Maven属性resource.delimiter来覆盖它。)

通过继承spring-boot-starter-parent使用spring-boot

<parent> 
	<groupId> org.springframework.boot </ groupId> 
	<artifactId> spring-boot-starter-parent </ artifactId> 
	<version> 2.1.6.RELEASE < / version> 
</ parent>

不通过继承spring-boot-starter-parent使用spring-boot

不是每个人都喜欢继承spring-boot-starter-parent。比如可能需要继承本公司的父依赖,或者想明确声明所有Maven配置。

如果您不想继承spring-boot-starter-parent,可以通过使用scope=import依赖项来保持依赖管理的好处 ,如下所示:

<dependencyManagement>
	<dependencies>
		<dependency>
			<!-- Import dependency management from Spring Boot -->
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.6.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

通过dependencyManagement继承和通过parent继承的区别:

  • parent(全部继承)spring-boot-starter-parent时项目中没有引用的依赖,仍然会从父项目中继承该依赖项。
  • dependencyManagement只声明依赖,并不实现引入,只有在子项目中引入该依赖项,并且没有指定具体版本时,才会从父项目中继承该项,version和scope都读取自父pom。另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

使用Spring Boot Maven插件

Spring Boot包含一个Maven插件,可以将项目打包为可执行jar。如果要使用它,请将插件添加到pom中,如以下示例所示:

<build> 
	<plugins> 
		<plugin> 
			<groupId> org.springframework.boot </ groupId> 
			<artifactId> spring-boot-maven-plugin </ artifactId> 
		</ plugin> 
	</ plugins> 
</ build>

Gradle

要了解如何将Spring Boot与Gradle一起使用,请参阅Spring Boot的Gradle插件的文档:Gradle使用

Spring Boot application starters

Spring Boot提供了一些快速启动项:

依赖名称功能
spring-boot-starter核心启动器,包括自动配置支持,日志记录和YAML
spring-boot-starter-activemq使用Apache ActiveMQ进行JMS消息传递的基础依赖
spring-boot-starter-amqp使用Spring AMQP和Rabbit MQ的基础依赖
spring-boot-starter-aop使用Spring AOP和AspectJ进行面向方面编程的基础依赖
spring-boot-starter-artemis使用Apache Artemis进行JMS消息传递的基础依赖
spring-boot-starter-batch使用Spring Batch的基础依赖
spring-boot-starter-cache使用Spring Framework的缓存支持的基础依赖
spring-boot-starter-cloud-connectors使用Spring Cloud Connectors的基础依赖简化了Cloud Foundry和Heroku等云平台中的服务连接
spring-boot-starter-data-cassandra使用Cassandra分布式数据库和Spring Data Cassandra的基础依赖
spring-boot-starter-data-cassandra-reactive使用Cassandra分布式数据库和Spring Data Cassandra Reactive的基础依赖
spring-boot-starter-data-couchbase使用Couchbase面向文档的数据库和Spring Data Couchbase的基础依赖
spring-boot-starter-data-couchbase-reactive使用Couchbase面向文档的数据库和Spring Data Couchbase Reactive的基础依赖
spring-boot-starter-data-elasticsearch使用Elasticsearch搜索和分析引擎以及Spring Data Elasticsearch的基础依赖
spring-boot-starter-data-jdbc使用Spring Data JDBC的基础依赖
spring-boot-starter-data-jpa将Spring Data JPA与Hibernate一起使用的基础依赖
spring-boot-starter-data-ldap使用Spring Data LDAP的基础依赖
spring-boot-starter-data-mongodb使用MongoDB面向文档的数据库和Spring Data MongoDB的基础依赖
spring-boot-starter-data-mongodb-reactive使用MongoDB面向文档的数据库和Spring Data MongoDB Reactive的基础依赖
spring-boot-starter-data-neo4j使用Neo4j图形数据库和Spring Data Neo4j的基础依赖
spring-boot-starter-data-redis在Spring Data Redis和Lettuce客户端上使用Redis键值数据存储的基础依赖
spring-boot-starter-data-redis-reactive使用带有Spring Data Redis被动的Redis键值数据存储和Lettuce客户端的基础依赖
spring-boot-starter-data-rest使用Spring Data REST通过REST公开Spring Data存储库的基础依赖
spring-boot-starter-data-solr在Spring Data Solr中使用Apache Solr搜索平台的基础依赖
spring-boot-starter-freemarker使用FreeMarker视图构建MVC Web应用程序的基础依赖
spring-boot-starter-groovy-templates使用Groovy模板视图构建MVC Web应用程序的基础依赖
spring-boot-starter-hateoas使用Spring MVC和Spring HATEOAS构建基于超媒体的RESTful Web应用程序的基础依赖
spring-boot-starter-integration使用Spring Integration的基础依赖
spring-boot-starter-jdbc将JDBC与HikariCP连接池一起使用的基础依赖
spring-boot-starter-jersey使用JAX-RS和Jersey构建RESTful Web应用程序的基础依赖。替代spring-boot-starter-web
spring-boot-starter-jooq使用jOOQ访问SQL数据库的基础依赖。替代spring-boot-starter-data-jpa或spring-boot-starter-jdbc
spring-boot-starter-json阅读和写作json的基础依赖
spring-boot-starter-jta-atomikos使用Atomikos进行JTA交易的基础依赖
spring-boot-starter-jta-bitronix使用Bitronix进行JTA事务的基础依赖
spring-boot-starter-mail使用Java Mail和Spring Framework的电子邮件发送支持的基础依赖
spring-boot-starter-mustache使用Mustache视图构建Web应用程序的基础依赖
spring-boot-starter-oauth2-client使用Spring Security的OAuth2 / OpenID Connect客户端功能的基础依赖
spring-boot-starter-oauth2-resource-server使用Spring Security的OAuth2资源服务器功能的基础依赖
spring-boot-starter-quartz使用Quartz调度程序的基础依赖
spring-boot-starter-security使用Spring Security的基础依赖
spring-boot-starter-test使用JUnit,Hamcrest和Mockito等库来测试Spring Boot应用程序的基础依赖
spring-boot-starter-thymeleaf使用Thymeleaf视图构建MVC Web应用程序的基础依赖
spring-boot-starter-validation使用Java Bean Validation和Hibernate Validator的基础依赖
spring-boot-starter-web使用Spring MVC构建Web(包括RESTful)应用程序的基础依赖。使用Tomcat作为默认嵌入式容器
spring-boot-starter-web-services使用Spring Web Services的基础依赖
spring-boot-starter-webflux使用Spring Framework的Reactive Web支持构建WebFlux应用程序的基础依赖
spring-boot-starter-websocket使用Spring Framework的WebSocket支持构建WebSocket应用程序的基础依赖
spring-boot-starter-actuator使用Spring Boot的Actuator的基础依赖,它提供生产就绪功能,帮助您监控和管理您的应用程序
spring-boot-starter-jetty使用Jetty作为嵌入式servlet容器的入门。替代spring-boot-starter-tomcat
spring-boot-starter-log4j2使用Log4j2进行日志记录的入门。替代spring-boot-starter-logging
spring-boot-starter-logging使用Logback进行日志记录的入门。默认日志启动器
spring-boot-starter-reactor-netty使用Reactor Netty作为嵌入式响应式HTTP服务器的基础依赖。
spring-boot-starter-tomcat使用Tomcat作为嵌入式servlet容器的基础依赖。使用的默认servlet容器启动器spring-boot-starter-web
spring-boot-starter-undertow使用Undertow作为嵌入式servlet容器的基础依赖。替代spring-boot-starter-tomcat
SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/maven/jyrepo/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/maven/jyrepo/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 2025-03-27 08:39:36.894 INFO 17776 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$e133a5ad] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-03-27 08:39:37.404 ERROR 17776 --- [ main] o.s.boot.SpringApplication : Application run failed java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application-dev.yml' at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:524) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:462) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:443) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE] at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null$6(ConfigFileApplicationListener.java:425) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
03-28
<think>嗯,用户遇到了SLF4J警告和一个Spring Boot启动失败的异常。首先,我需要分析这两个问题是否相关。SLF4J提示存在多个绑定,这通常是因为项目中引入了多个日志框架的实现,比如同时有Logback和Log4j的依赖。这时候SLF4J无法确定使用哪一个,所以会选择一个默认的,但可能会导致日志配置冲突或者不生效。用户日志里显示有两个绑定:logback-classic和slf4j-log4j12,这显然冲突了。 接下来是Spring Boot启动失败的错误,报的是加载application-dev.yml文件失败。这个错误可能和配置文件的格式有关,比如缩进不正确、冒号后面缺少空格,或者键值对写错了。也有可能文件本身损坏,或者编码问题。不过用户提供的错误堆栈显示是在ConfigFileApplicationListener里加载属性源失败,所以首先应该检查application-dev.yml的内容是否正确。 现在需要确定这两个问题是否有联系。一般来说,SLF4J的绑定问题通常不会直接导致配置文件加载失败,除非日志框架初始化过程中出现了异常,但根据错误信息,加载失败是在SpringApplication运行时发生的,可能是在启动过程中较早的阶段。所以可能这两个问题是独立的,需要分别处理。 对于SLF4J的多个绑定,解决办法是排除掉其中一个依赖。比如用户的项目中同时引入了logback-classic和slf4j-log4j12,应该通过Maven或Gradle排除其中一个。比如如果使用Logback作为日志实现,就需要排除掉slf4j-log4j12;反之如果想用Log4j 1.x,则排除Logback。不过需要注意的是,Log4j 1.x已经过时,可能存在安全漏洞,建议升级到Log4j2或者使用Logback。 然后是application-dev.yml加载失败的问题。用户需要检查该文件是否存在,路径是否正确,语法是否正确。YAML对格式要求严格,缩进必须用空格,不能用Tab,键值对的冒号后需要有空格。此外,还要检查是否有特殊字符未转义,或者结构错误,比如重复的键。可以使用在线的YAML验证工具或者IDE的插件来检查语法是否正确。 可能用户没有在配置文件中正确设置属性,导致Spring Boot无法解析。比如,某个属性没有正确缩进,或者列表项格式错误。例如: 正确的YAML: server: port: 8080 错误的可能写成: server port: 8080 # 缺少冒号 或者: spring: datasource: url: jdbc:mysql://localhost/test username: root password: 但如果在某个层级缩进错误,会导致解析失败。此外,如果使用了不支持的配置项名称,或者环境变量没有正确设置,也可能导致问题。不过根据错误信息,最可能的是YAML文件本身的语法错误。 总结下来,用户需要处理两个问题:排除多余的日志绑定,并修复YAML配置文件。这两个问题需要分别解决,可能没有直接关联,但都需要处理才能使应用正常启动。</think>### 问题分析及解决方案 你遇到的日志包含两个关键问题: 1. **SLF4J警告**(存在多个日志框架绑定冲突) 2. **Spring Boot启动失败**(无法加载 `application-dev.yml` 配置文件) --- ### 一、SLF4J警告:`Class path contains multiple SLF4J bindings` #### 原因 SLF4J 是日志门面框架,需要与具体的日志实现(如 Logback、Log4j)配合使用。你的依赖中**同时包含两个日志实现**: - `logback-classic-1.2.3.jar`(Logback 实现) - `slf4j-log4j12-1.7.29.jar`(Log4j 1.x 实现) SLF4J 无法自动选择,因此会随机绑定一个实现(此处绑定到 Logback),但冲突可能导致日志行为异常。 #### 解决方法 **步骤 1:检查依赖关系** 通过 Maven/Gradle 的依赖树工具,定位冲突的依赖来源: - **Maven 命令**: ```bash mvn dependency:tree ``` - **Gradle 命令**: ```bash gradle dependencies ``` **步骤 2:排除多余依赖** 假设你希望保留 Logback(Spring Boot 默认使用 Logback),则需排除 `slf4j-log4j12`: - **Maven 配置示例**: ```xml <dependency> <groupId>某个依赖的GroupId</groupId> <artifactId>某个依赖的ArtifactId</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> ``` - **Gradle 配置示例**: ```groovy implementation('某个依赖') { exclude group: 'org.slf4j', module: 'slf4j-log4j12' } ``` --- ### 二、Spring Boot启动失败:`Failed to load property source` #### 原因 Spring Boot 无法解析 `classpath:/application-dev.yml` 文件,常见原因包括: 1. **YAML 语法错误**(如缩进错误、冒号后缺少空格) 2. **文件路径错误**(如文件未放置在 `src/main/resources` 下) 3. **配置项冲突**(如重复键名、不支持的属性) #### 解决方法 **步骤 1:验证 YAML 语法** - 使用在线工具(如 [YAML Lint](https://www.yamllint.com/))检查 `application-dev.yml` 格式。 - **常见错误示例**: ```yaml # 错误:冒号后缺少空格 key:value # 正确 key: value # 错误:缩进使用 Tab 而非空格 server: port: 8080 # 正确 server: port: 8080 ``` **步骤 2:检查文件位置** 确保 `application-dev.yml` 位于以下目录: ``` src/main/resources/application-dev.yml ``` **步骤 3:检查 Spring Profile 激活配置** 在 `application.yml` 中确认已激活 `dev` Profile: ```yaml spring: profiles: active: dev ``` **步骤 4:排查特殊字符** 避免在 YAML 中使用未转义的特殊字符(如 `@`、`!`),必要时用引号包裹: ```yaml password: "123@abc" ``` --- ### 三、关联性分析 1. **SLF4J 警告**通常不会直接导致 Spring Boot 启动失败,但需解决以避免日志混乱。 2. **启动失败的根本原因**是 `application-dev.yml` 加载失败,需优先修复配置文件。 --- ### 总结 1. 通过依赖排除解决 SLF4J 绑定冲突。 2. 修复 `application-dev.yml` 的语法或路径问题。 3. 重新编译并启动应用,验证问题是否解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值