Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导类路径配置文件覆盖外部application.properties_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring引导类路径配置文件覆盖外部application.properties

Java Spring引导类路径配置文件覆盖外部application.properties,java,spring,spring-boot,Java,Spring,Spring Boot,我希望Spring加载一个外部配置文件(如果存在),否则使用src/main/resources中提供的once 当前设置: src/main/resources/application.properties src/main/resources/application-dev.properties src/main/resources/application-prod.properties /this/is/an/external/dir/application-dev.properties

我希望Spring加载一个外部配置文件(如果存在),否则使用src/main/resources中提供的once

当前设置:

src/main/resources/application.properties
src/main/resources/application-dev.properties
src/main/resources/application-prod.properties

/this/is/an/external/dir/application-dev.properties
与之类似,我添加了以下注释:

@EnableFeignClients
@SpringBootApplication
@EnableEncryptableProperties
@PropertySources({ //
    @PropertySource("classpath:application.properties"), //
    @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true) //
})
public class Application extends SpringBootServletInitializer {

   // ....
}
并在my application.properties中具有以下条目:

external.config=/this/is/an/external/dir/application-dev.properties
我还可以看到外部conifguration文件已被拾取。我的问题是类路径属性文件中的每个条目都会覆盖外部条目。也就是说,不从 /此/is/an/external/dir/application-dev.properties条目来自 获取src/main/resources/application-dev.properties


如何修改代码,使外部文件覆盖类路径文件的条目?

只需更改导入顺序即可。 比如:

<context:property-placeholder file-encoding="UTF-8"
                            location="${YOUR_CUSTOM_PATH}/global.properties ,
                                      ${YOUR_CUSTOM_PATH}/local.properties" ignore-unresolvable="true"/>

在这种情况下,
local.properties
将覆盖
global.properties

的属性,只需更改导入顺序即可。 比如:

<context:property-placeholder file-encoding="UTF-8"
                            location="${YOUR_CUSTOM_PATH}/global.properties ,
                                      ${YOUR_CUSTOM_PATH}/local.properties" ignore-unresolvable="true"/>

在此情况下,<代码>本地.Projs重写<代码> Global。Projs

> P>。因为您只想开发外部配置,您可以考虑将外部属性源设置为项目IDE运行配置中的命令行参数。

--spring.config.location=/this/is/an/external/dir/application-dev.properties

以上述方式指定的属性源重写类路径中的Apultual.Projts属性。

,因为您只想在开发中使用外部配置,您可以考虑将外部属性源设置为项目IDE运行配置中的命令行参数。

--spring.config.location=/this/is/an/external/dir/application-dev.properties
以上述方式指定的属性源将重写类路径中存在的application.properties

我希望Spring加载一个外部配置文件(如果存在),否则使用src/main/resources中提供的once

这是Spring Boot的默认行为。所以您只需要正确地启用它

首先,删除下一个注释,它们可能会中断/覆盖默认机制:

@PropertySources({ //
    @PropertySource("classpath:application.properties"), //
    @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true) //
})
然后将文件的位置指定为命令行参数:

java -jar myapp.jar --spring.config.additional-location=/this/is/an/external/dir/
更多信息请点击此处:

然后将配置重命名为
application.properties
,或者通过另一个环境变量指定
dev
配置文件(文件名中的后缀):

-Dspring-boot.run.profiles=dev
相关问题:

我希望Spring加载一个外部配置文件(如果存在),否则使用src/main/resources中提供的once

这是Spring Boot的默认行为。所以您只需要正确地启用它

首先,删除下一个注释,它们可能会中断/覆盖默认机制:

@PropertySources({ //
    @PropertySource("classpath:application.properties"), //
    @PropertySource(value = "file:${external.config}", ignoreResourceNotFound = true) //
})
然后将文件的位置指定为命令行参数:

java -jar myapp.jar --spring.config.additional-location=/this/is/an/external/dir/
更多信息请点击此处:

然后将配置重命名为
application.properties
,或者通过另一个环境变量指定
dev
配置文件(文件名中的后缀):

-Dspring-boot.run.profiles=dev

相关问题:

对我来说不太合适。。我认为(对我来说)问题在于解决的顺序:我在外部文件和src/main/resource文件中具有相同的属性。对于开发,我需要/src/main/resources文件。但是对于构建应用程序,我希望将application-dev.properties放在一个外部位置。这可能是区分开发和生产环境的一个很好的资源,而开发和生产环境对我来说并不是很好。。我认为(对我来说)问题在于解决的顺序:我在外部文件和src/main/resource文件中具有相同的属性。对于开发,我需要/src/main/resources文件。但是对于构建应用程序,我希望将application-dev.properties放在一个外部位置。这可能是区分开发和生产环境的一个很好的资源