簡體   English   中英

在Tomcat中為Spring Boot應用程序訪問externalize application.properties?

[英]access externalize application.properties in Tomcat for Spring boot application?

我有春季啟動應用程序。

我試圖從tomcat位置訪問application.properties文件。

我遵循此鏈接: 如何在Tomcat Web服務器中外部化Spring的application.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軟件基金會\\ 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軟件基金會\\ 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).

讓我知道如何解決此問題。或者是否還有其他方法可以使用Spring Boot從tomcat位置訪問屬性文件。

請加 -

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

要么

在課程上方使用注釋-

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

從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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM