如何将Spring Boot application.properties外部化到tomcat / lib文件夹 [英] How to externalize Spring Boot application.properties to tomcat/lib folder

查看:714
本文介绍了如何将Spring Boot application.properties外部化到tomcat / lib文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个免费的,可部署的war,myapp1.war,它可以从tomcat / lib文件夹中检索配置文件。
因为我有其他Web应用程序共存于同一个Tomcat:myapp2.war,myapp3.war,我需要这个布局:

I need a configuration free, deployable war, myapp1.war that can retrieve the configuration files from the tomcat/lib folder. As I have other web applications coexisting on the same Tomcat: myapp2.war, myapp3.war, I need this layout:

tomcat/lib/myapp1/application.properties
tomcat/lib/myapp2/application.properties
tomcat/lib/myapp3/application.properties

这样我可以在战争中构建没有任何属性文件的war文件,并在任何服务器上部署。

This way I can build the war files without any properties files inside the war and deploy on any server.

我已阅读 Spring文档,但它解释了如何在作为jar运行时设置位置:

I have read the Spring documentation but it explains how to set the location when running as a jar:

java -jar myapp.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

我无法弄清楚如何在多个共存战争文件的情况下这样做。

I cannot figure out how to do this for the case of multiple coexisting war files.

我想知道这是否是pos或者我应该放弃Spring Boot并回到传统的Spring MVC应用程序。

I would like to know if this is possible or should I give up on Spring Boot and go back to the traditional Spring MVC applications.

推荐答案

解决方案可能是加载application- {profile} .properties as @PropertySource annotations as this 问题建议,但是日志系统不会工作,你可以在文档

A solution could be to load application-{profile}.properties as @PropertySource annotations as this question suggests, but then the logging system wont work, as you can see in the documentation.


日志记录系统在应用程序生命周期早期初始化
和因为这样的日志属性将不会在通过@PropertySource注释加载的属性文件
中找到。

The logging system is initialized early in the application lifecycle and as such logging properties will not be found in property files loaded via @PropertySource annotations.

这意味着您的日志记录属性在application- {profiles} .properties中:

This means that your logging properties in application-{profiles}.properties like:

logging.config=classpath:myapp1/logback.xml
logging.path = /path/to/logs
logging.file = myapp1.log

将被忽略并且日志记录系统无法工作。

will be ignored and the logging system wont work.

为了解决这个问题,我在开始配置应用程序时使用了SpringApplicationBuilder.properties()方法来加载属性。在那里,我设置了Spring Boot使用的'spring.config.location'来加载所有的应用程序 - {profiles} .properties:

To solve this I have used the SpringApplicationBuilder.properties() method to load properties at the beginning, when the application is configured. There I set the 'spring.config.location' used by Spring Boot to load all the application-{profiles}.properties:

public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
        return springApplicationBuilder
                .sources(Application.class)
                .properties(getProperties());
    }

    public static void main(String[] args) {

        SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class)
                .sources(Application.class)
                .properties(getProperties())
                .run(args);
    }

   static Properties getProperties() {
      Properties props = new Properties();
      props.put("spring.config.location", "classpath:myapp1/");
      return props;
   }
}

然后我从src / main移动了属性文件/资源到src / main / resources / myapp1

Then I have moved the properties files from src/main/resources to src/main/resources/myapp1

.
├src
| └main
|   └resources
|     └myapp1
|       └application.properties
|       └application-development.properties
|       └logback.xml
└─pom.xml

在pom.xml中我必须将嵌入式tomcat库的范围设置为提供。
此外,要从最终的战争中排除src / main / resources / myapp1中的所有属性文件,并生成一个免费配置的可配置战争:

In the pom.xml I have to set the scope of embedded tomcat libraries as "provided". Also, to exclude all properties files in src/main/resources/myapp1 from the final war and generate a configuration free, deployable war:

    <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
            <packagingExcludes>
              **/myapp1/
            </packagingExcludes>
        </configuration>
    </plugin>

然后在Tomcat我有

Then in Tomcat I have

├apache-tomcat-7.0.59
 └lib
   ├─myapp1
   |  └application.properties        
   |  └logback.xml
   └─myapp2
     └application.properties
     └logback.xml

现在我可以生成配置免费战争并将其放入apache-tomcat-7.0.59 / webapps文件夹中。属性文件将使用类路径解析,每个webapp独立:

Now I can generate the configuration free war and drop it into the apache-tomcat-7.0.59/webapps folder. Properties files will be resolved using the classpath, independently for each webapp:

   apache-tomcat-7.0.59/lib/myapp1
   apache-tomcat-7.0.59/lib/myapp2
   apache-tomcat-7.0.59/lib/myapp3

这篇关于如何将Spring Boot application.properties外部化到tomcat / lib文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆