mem*_*und 41 java spring spring-boot
我正在使用spring boot并application.properties在开发过程中选择一个数据库@Configuration @Profile("dev").
spring.profiles.active=dev
spring.config.location=file:d:/application.properties
Run Code Online (Sandbox Code Playgroud)
在生产过程中,我想创建一个应该加载的应用程序上下文之外的文件,然后使用d:/application.properties激活不同的配置文件:
spring.profiles.active=production
Run Code Online (Sandbox Code Playgroud)
结果:当我启动应用程序时,配置仍然是dev,因此不会考虑生产属性文件的其他位置.我错过了什么吗?
弹簧靴1.1.0.BUILD-SNAPSHOT
注意:这个问题不是关于tomcat的.
Kal*_*oni 18
我知道你问过怎么做.
但答案是,你不应该这样做.
您可以拥有application.properties application-default.properties application-dev.properties等
您可以通过命令行参数将配置文件切换到jvm
你可以使用@TestPropertySource在测试时覆盖一些东西
理想情况下,所有内容都应该在源代码管理中,这样就不会有任何意外 - 您如何知道服务器位置中存在哪些属性,以及缺少哪些属性.如果开发人员介绍新东西会发生什么
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Spring boot已经为你提供了足够的方法来做到这一点.
Sau*_*abh 11
你也可以使用 @PropertySources
@PropertySources({
@PropertySource(value = "classpath:application.properties"),
@PropertySource(value = "file:/user/home/external.properties", ignoreResourceNotFound = true)
})
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
Gab*_*uiu 10
我不确定您是否可以动态更改配置文件.
为什么不将spring.config.location属性设置为所需的外部位置的内部属性文件,并且该位置(jar外部)的属性文件是否设置了spring.profiles.active属性?
更好的是,有一个特定于dev配置文件的内部属性文件(具有spring.profiles.active = dev)并保持原样,当你想在生产中部署时,为你的属性文件指定一个新的位置,它有spring .profiles.active = PROD:
java -jar myjar.jar --spring.config.location=D:\wherever\application.properties
Run Code Online (Sandbox Code Playgroud)
从 Spring Boot 2 开始,您将不得不使用
--spring.config.additional-location=production.properties
Run Code Online (Sandbox Code Playgroud)
使用 Spring Boot 2.2.2.Release 进行更新。
完整示例,https://www.surasint.com/spring-boot-override-property-example/
假设,在您的 jar 文件中,您的 application.properties 包含以下两行:
server.servlet.context-path=/test
server.port=8081
Run Code Online (Sandbox Code Playgroud)
然后,在生产中,您希望覆盖 server.port=8888 但您不想覆盖其他属性。
首先,您创建另一个文件,例如 override.properties 并在线设置这一行:
server.port=8888
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样启动jar
java -jar spring-boot-1.0-SNAPSHOT.jar --spring.config.location=classpath:application.properties,/opt/somewhere/override.properties
Run Code Online (Sandbox Code Playgroud)
更新:这是春天的错误,请参见此处
jar外部的应用程序属性必须位于以下位置之一,然后一切正常。
21.2 Application property files
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdir of the current directory.
The current directory
A classpath /config package
The classpath root
Run Code Online (Sandbox Code Playgroud)
因此,例如,当您不想指定cmd行参数并且不在基本app.props中使用spring.config.location时,这应该可以工作:
d:\yourExecutable.jar
d:\application.properties
or
d:\yourExecutable.jar
d:\config\application.properties
Run Code Online (Sandbox Code Playgroud)
请参阅spring外部配置文档
更新:您可以将\ @Configuration与\ @PropertySource一起使用。根据此处的文档,您可以在任何地方指定资源。您应该小心一点,在加载哪个配置时要确保您的产品获胜。
我发现以下内容对我有用:
java -jar my-awesome-java-prog.jar --spring.config.location=file:/path-to-config-dir/
Run Code Online (Sandbox Code Playgroud)
与file:添加。
后期编辑
当然,这个命令行永远不会像在生产中那样运行。
而是我有
shell源代码控制中的脚本,带有占位符的命令的所有部分都可以更改(jar 的名称、配置的路径...)ansible将部署shell脚本并用实际值替换占位符的部署脚本。| 归档时间: |
|
| 查看次数: |
92782 次 |
| 最近记录: |