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

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

问题描述

我需要一个无需配置、可部署的war,myapp1.war,它可以从tomcat/lib 文件夹中检索配置文件.由于我在同一个 Tomcat 上共存了其他 Web 应用程序:myapp2.war、myapp3.war,我需要这样的布局:

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

这样我就可以在没有任何属性文件的情况下构建战争文件并部署在任何服务器上.

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

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

对于多个并存的战争文件,我无法弄清楚如何做到这一点.

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

解决方案

一个解决方案可能是加载 application-{profile}.properties as @PropertySource annotations as this 问题 建议,但随后日志系统将无法工作,正如您在 文档.

<块引用>

日志系统在应用程序生命周期的早期初始化并且因此在属性文件中找不到这样的日志属性通过@PropertySource 注释加载.

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

logging.config=classpath:myapp1/logback.xmllogging.path =/path/to/logslogging.file = myapp1.log

将被忽略,日志系统将无法工作.

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

public class Application extends SpringBootServletInitializer {@覆盖受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder springApplicationBuilder){返回 springApplicationBuilder.sources(Application.class).properties(getProperties());}公共静态无效主(字符串 [] args){SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Application.class).sources(Application.class).properties(getProperties()).run(args);}静态属性 getProperties() {Properties props = new Properties();props.put("spring.config.location", "classpath:myapp1/");返回道具;}}

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

<预><代码>.├src|└主要|└资源|└我的应用1|└application.properties|└application-development.properties|└logback.xml└─pom.xml

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

 <插件><artifactId>maven-war-plugin</artifactId><version>2.6</version><配置><failOnMissingWebXml>false</failOnMissingWebXml><包装不包括>**/我的应用程序1/</packagingExcludes></配置></插件>

然后在Tomcat中我有

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

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

 apache-tomcat-7.0.59/lib/myapp1apache-tomcat-7.0.59/lib/myapp2apache-tomcat-7.0.59/lib/myapp3

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

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

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.

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.

解决方案

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.

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.

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.

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;
   }
}

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

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>

Then in Tomcat I have

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

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天全站免登陆