访问Tomcat中用于Spring引导应用程序的外部化application.properties? [英] access externalize application.properties in Tomcat for Spring boot application?

查看:195
本文介绍了访问Tomcat中用于Spring引导应用程序的外部化application.properties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Spring Boot应用程序.

i have spring boot application.

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

i am trying to access application.properties file from tomcat location.

我点击了以下链接:如何将应用程序外部化Spring的Tomcat Web服务器中的.properties?

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

但是当我运行应用程序或构建应用程序时.由于未找到数据库连接,因此显示错误.

But when i am running the application or build the application. it is showing error, because it's not finding the db connection.

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).

让我知道如何解决此问题.或者,如果还有其他访问方式 使用spring boot从tomcat位置下载属性文件.

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文件.那么我们需要遵循以下步骤

To access application.properties file from tomcat directory. then we need to follow below steps

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

Need to add plugin in pom.xml. which means it'll ignore the workspace application.properties file after deployment

<!-- 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文件.

Need to copy the application.properties file to tomcat directory lib location. then we need to change the ServletInitializer.java file.

"classpath:mortgage-api/"意味着我们需要在tomcat lib文件夹中创建一个名称为mortgage-api的文件夹,并将application.properties文件复制到该位置.检查代码注释.

"classpath:mortgage-api/" means we need to create a folder with name mortgage-api in tomcat lib folder and will copy application.properties file to this location. check the code comment.

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,然后构建战争文件mvn install

then mvn clean, then build war file mvn install

这篇关于访问Tomcat中用于Spring引导应用程序的外部化application.properties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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