gpt4 book ai didi

java - 当我指定 spring.config.location 时,Spring Boot 2.x 不扫描 application.properties

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:20:08 30 4
gpt4 key购买 nike

所以我只是在玩 Spring Boot 2.0.4,今天我注意到了这一点。不确定我是否错过了什么。请帮我检查一下。

Spring 申请

@SpringBootApplication
@EnableScheduling
public class Application extends SpringBootServletInitializer {

application.properties(位于 src/main/resources)

server.port=8088

使用Intellij启动项目

2018-08-17 12:11:05 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8088 (http) with context path ''

使用java命令行启动项目:

java -jar sample.jar --spring.config.location=D:\config\ --spring.profiles.active=dev

应用程序没有使用配置的端口

2018-08-17 11:25:25 INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path ''

貌似Spring Boot 2.0忽略了默认的properties文件(注意:这个配置只在applications.properties中没有其他地方,所以不会被其他配置文件覆盖)

最佳答案

您指定 spring.config.location 来启动 uber-jar,从 Spring Boot 2 开始,指定此参数将替换 Spring 使用的默认位置,如 in the documentation 所述.

When custom config locations are configured by using spring.config.location, they replace the default locations. For example, if spring.config.location is configured with the value classpath:/custom-config/,file:./custom-config/, the search order becomes the following:

  1. file:./custom-config/

  2. classpath:custom-config/

所以我认为您必须在 spring.config.location 中显式添加 application.properties :

--spring.config.location=D:\config\,classpath:\application.properties

或者作为替代使用 spring.config.additional-location 例如 --spring.config.additional-location=D:\config\ 而不是 spring.config.location 将添加而不是替换位置 as stated by the documentation :

Alternatively, when custom config locations are configured by using spring.config.additional-location, they are used in addition to the default locations.

关于java - 当我指定 spring.config.location 时,Spring Boot 2.x 不扫描 application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888310/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com