【问题标题】:SpringBoot: external application.properties not found in deploying warSpring Boot:部署战争时找不到外部application.properties
【发布时间】:2018-01-31 17:54:07
【问题描述】:

我正在尝试通过将war部署到tomcat容器来通过外部配置来配置我的Spring Boot应用程序中的属性。

我的application.properties 文件保存在tomcat_folder/conf/myappname/application.properties

我正在使用此属性文件加载一些 aws 配置和数据库详细信息,例如 spring.datasource.url 等,并希望使这些属性可用于整个应用程序,并且可以像 @Value("${aws.access_key_id}") 一样访问

我还通过添加参数更改了tomcat conf中的Context.xml文件:

<Parameter name="spring.config.location" value="${catalina.base}/conf/myappname/application.properties" />

我的主要应用程序类看起来像:

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class);
    }

    public static void main(String[] args) {        
        SpringApplication.run(MyApplication.class, args);        
    }
}

构建并部署war到webapps文件夹后,当我启动服务器时,我得到了错误

Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

不确定还需要配置什么。

【问题讨论】:

    标签: java spring tomcat spring-boot application.properties


    【解决方案1】:

    我已经测试了你的配置步骤,对我来说看起来不错。

    您可以使用其他方式来测试您的配置:

    1. application.properties文件放入tomcat_folder/lib/application.properties
    2. 删除Context.xml中的以下脚本。

    &lt;Parameter name="spring.config.location"value="${catalina.base}/conf/myappname/application.properties" /&gt;

    1. 启动服务器。

    另外,您可以编辑catalina.properties

    shared.loader=${catalina.base}/conf/myappname/

    并将您的application.properties 文件保存在

    tomcat_folder/conf/myappname/application.properties

    【讨论】:

    • 将属性放在lib 文件夹下有效。你能解释一下吗?
    • 你的配置方式是对的,请仔细检查是否有任何遗漏或错别字。
    • 我有一个@PropertySource(value={classpath:application.properties}。我想这就是问题所在。
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2022-11-10
    • 2019-03-21
    • 1970-01-01
    • 2017-09-21
    • 2016-08-16
    • 2018-07-05
    • 2015-10-28
    相关资源
    最近更新 更多