[英]How to override properties from application.yml when using external tomcat?
I deploy my app in external tomcat as a WAR file because there are multiple applications run on the same tomcat.我将我的应用程序作为 WAR 文件部署在外部 tomcat 中,因为有多个应用程序在同一个 tomcat 上运行。
In my application.yml
inside resources
directory, I have (among other properties):在resources
目录内的application.yml
中,我有(以及其他属性):
logging:
file: MY_APP.log
I want to override this value when the app will run into external tomcat.当应用程序运行到外部 tomcat 时,我想覆盖这个值。 I created application-myapp.yml into CATALINA_BASE/conf/application-myapp.yml
with the following contents:我在CATALINA_BASE/conf/application-myapp.yml
中创建了 application-myapp.yml,内容如下:
logging:
file: ${catalina.base}/logs/MY_APP.log
And in my main class:在我的主要 class 中:
@SpringBootApplication
@PropertySource({"classpath:application.yml", "file:${catalina.base}/conf/application-myapp.yml"})
public class MyApp extends SpringBootServletInitializer{
}
but the value is not overriden and MY_APP.log is created in /bin directory where I execute startup.bat
which means the logging.file property is not overriden by conf/application-myapp.yml
.但是该值没有被覆盖,并且 MY_APP.log 是在我执行startup.bat
的 /bin 目录中创建的,这意味着 logging.file 属性不会被conf/application-myapp.yml
。
NOTE that I need this for some other properties.请注意,我需要它用于其他一些属性。 Like JDBC url, etc. logging.file is just an example!像JDBC url等,logging.file只是一个例子!
What I want is to have a permanent application-myapp.yml
into tomcat/conf directory with some properties configured to work in production.我想要的是在 tomcat/conf 目录中有一个永久的application-myapp.yml
,其中一些属性配置为在生产中工作。 These values to override the ones (where property exists in both config files) from resources/application.yml
.这些值将覆盖resources/application.yml
中的值(两个配置文件中都存在属性)。
I tried a lot of things.我尝试了很多东西。 I tried also to have .properties
file, but no luck.我也尝试拥有.properties
文件,但没有运气。 I also tried the following:我还尝试了以下方法:
@PropertySources(
@PropertySource("classpath:application.yml"),
@PropertySource(value = "file:${catalina.base}/conf/application-myapp.yml", ignoreResourceNotFound = true)
)
but still...no luck.但仍然......没有运气。 I also tried different order in @PropertySource annotations.我还在 @PropertySource 注释中尝试了不同的顺序。
How can I achieve this in Spring Boot 1.5.8.RELEASE
version?如何在Spring Boot 1.5.8.RELEASE
版本中实现这一点?
For example, if I have:例如,如果我有:
resources/application.yml:
property1: value1
property2: value2
property3: value3
CATALINA_BASE/conf/application-myapp.yml:
property2: value4
Application will start:申请将开始:
property1: value1
property2: value4 #<-- here, overriden by application-myapp.yml
property3: value3
To use the external config, try setting the SPRING_CONFIG_LOCATION environment variable like below:要使用外部配置,请尝试设置 SPRING_CONFIG_LOCATION 环境变量,如下所示:
export SPRING_CONFIG_LOCATION=${catalina.base}/conf/application-myapp.yml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.