java - 在 Tomcat 中访问 externalize application.properties for Spring boot 应用程序?

标签 java spring-boot tomcat properties application.properties

我有 spring boot 应用程序。

我正在尝试从 tomcat 位置访问 application.properties 文件。

我点击了这个链接:How to externalize application.properties in Tomcat webserver for Spring?

MortgageLoanApiApplication

package com.Mortgage.MortgageLoanAPI;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MortgageLoanApiApplication {

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "application");
        SpringApplication.run(MortgageLoanApiApplication.class, args);
    }

}

ServletInitializer

package com.Mortgage.MortgageLoanAPI;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MortgageLoanApiApplication.class).properties("spring.config.name: application");
    }

}

C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.9\bin\setenv.sh

export spring_config_location=C:/Program Files/Apache Software Foundation/Apache Tomcat 8.0.9/conf/

C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.9\conf\application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/mortgage_loan
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

但是当我运行应用程序或构建应用程序时。它显示错误,因为它没有找到数据库连接。

2019-03-04 10:53:28.318  INFO 2872 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-04 10:53:28.325 ERROR 2872 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

Let me know how to fix this.Or If there is any other way to access properties file from tomcat location using spring boot.

最佳答案

从 tomcat 目录访问 application.properties 文件。那么我们需要按照以下步骤进行

需要在pom.xml中添加插件。这意味着它将在部署后忽略工作区 application.properties 文件

<!-- Added the below plugin to not include the application.properties inside the war -->
<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <packagingExcludes>
            **/application.properties/
        </packagingExcludes>
    </configuration>
</plugin>

需要将application.properties文件复制到tomcat目录lib位置。然后我们需要更改 ServletInitializer.java 文件。

"classpath:mortgage-api/" 表示我们需要在 tomcat lib 文件夹中创建一个名为 mortgage-api 的文件夹,并将 application.properties 文件复制到此地点。检查代码注释。

import java.util.Properties;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MortgageLoanApiApplication.class).properties(loadproperties());
    }

    /**
     * Added this method to load properties from the classpath while intializing the server app
     * location of the properties file should be inside tomcat directory:
     *    lib/mortgage-api/application.properties
     * @return
     */
    private Properties loadproperties() {
          Properties props = new Properties();
          props.put("spring.config.location", "classpath:mortgage-api/");
          return props;
       }

}

然后mvn clean,然后构建war文件mvn install

关于java - 在 Tomcat 中访问 externalize application.properties for Spring boot 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54977186/

相关文章:

spring - spring-data-mongodb/k8s “Database name must not contain slashes, dots, spaces, quotes, or dollar signs”

java - 修改 IntelliJ 为 Maven 项目的 Tomcat 或 Jetty 配置的资源创建符号链接(symbolic link)

java - 同一分片的所有 Solr 从属内存不足

java - 通过 Java 套接字发送带有 <APPLET> 标记的 html 文件

java - 为什么泛型方法和泛型类型有不同的类型引入语法?

Java 阻止列表实现

java - Spring Boot 修改默认 JSON 响应

java - spring 应用程序中的空存储库,即使所有对象都被 Autowiring 并且从未手动实例化

java - Tomcat 无法在主机上下文 server.xml 中设置 'source' 属性集

java - 如何从 run 方法中获取值?