栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将Spring Boot application.properties外部化为tomcat / lib文件夹

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将Spring Boot application.properties外部化为tomcat / lib文件夹

一个解决方案可能是按照此问题的]建议将application- {profile}
.properties加载为@PropertySource批注,但随后的日志记录系统将无法正常工作,如您在文档中所见。

日志记录系统在应用程序生命周期的早期进行了初始化,因此在通过@PropertySource批注加载的属性文件中找不到此类日志记录属性。

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

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

将被忽略,日志系统将无法正常工作。

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

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 / resources移至src / main / resources / myapp1

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

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

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

然后在Tomcat中

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

现在,我可以生成免配置的war并将其放到apache-tomcat-7.0.59 /
webapps文件夹中。属性文件将使用类路径(对于每个Web应用程序)独立解析:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号