简体   繁体   English

使用外部 tomcat 时如何覆盖 application.yml 中的属性?

[英]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.

相关问题 从 application.yml 读取属性时忽略 Spring 配置文件 - Spring profile is ignored when reading properties from application.yml 如何从 application.yml 映射时间属性? - How to map time properties from application.yml? 如何从 application.yml 文件注入属性? - How to inject properties from application.yml file? 如何在springboot中从application.yml中读取带有特殊字符的属性 - How to read properties with special characters from application.yml in springboot Spring Boot - 如何通过实现 BeforeAllCallback 的自定义扩展类设置或覆盖 application.yml 中定义的属性? - Spring Boot - How to set or override properties defined in application.yml via a custom extension class that implements BeforeAllCallback? 如何使用 docker run 命令为 Spring Boot 应用程序覆盖 application.yml 中的 spring 属性? - How can you override a spring property in application.yml for a Spring Boot application using the docker run command? 当我启动 Spring Boot 应用程序时,它没有从 application.yml 读取属性 - When I start my Spring boot application it is not reading properties from application.yml Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application 如何在 Spring Boot 中从 application.properties 转换为 application.yml? - How to convert from application.properties to application.yml in Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM