简体   繁体   English

在Tomcat中为Spring Boot应用程序访问externalize application.properties?

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

i have spring boot application. 我有春季启动应用程序。

i am trying to access application.properties file from tomcat location. 我试图从tomcat位置访问application.properties文件。

i followed this link : How to externalize application.properties in Tomcat webserver for Spring? 我遵循此链接: 如何在Tomcat Web服务器中外部化Spring的application.properties?

MortgageLoanApiApplication 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 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 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 Software Foundation\\Apache Tomcat 8.0.9\\conf\\application.properties 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

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

Let me know how to fix this.Or If there is any other way to access properties file from tomcat location using spring boot. 让我知道如何解决此问题。或者是否还有其他方法可以使用Spring Boot从tomcat位置访问属性文件。

Please add - 请加 -

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

or 要么

Use annotation above class - 在课程上方使用注释-

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})

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

To access application.properties file from tomcat directory. 从tomcat目录访问application.properties文件。 then we need to follow below steps 那么我们需要遵循以下步骤

Need to add plugin in pom.xml . 需要在pom.xml添加插件。 which means it'll ignore the workspace application.properties file after deployment 这意味着部署后它将忽略工作区的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>

Need to copy the application.properties file to tomcat directory lib location. 需要将application.properties文件复制到tomcat目录的lib位置。 then we need to change the ServletInitializer.java file. 那么我们需要更改ServletInitializer.java文件。

"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. "classpath:mortgage-api/"表示我们需要在tomcat的lib文件夹中创建一个名称为mortgage-api文件夹,并将application.properties文件复制到该位置。 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;
       }

}

then mvn clean , then build war file mvn install 然后mvn clean ,然后构建war文件mvn install

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Spring Boot application.properties外部化到tomcat / lib文件夹 - How to externalize Spring Boot application.properties to tomcat/lib folder 作为WAR在Springcat上部署的Spring Boot,如何针对不同配置文件(Dev,Test,Staging,Prod)外部化application.properties - Spring boot deployed as WAR on tomcat, How to Externalize application.properties for different profiles(Dev,Test,Staging,Prod) 如何在 Spring 的 Tomcat Web 服务器中外部化 application.properties? - How to externalize application.properties in Tomcat webserver for Spring? 在插件中通过Tomcat覆盖Spring Boot application.properties属性? - Overwriting Spring Boot application.properties properties in plugin and through Tomcat? 如何将Spring Boot中的application.properties文件外部化为外部依赖JAR? - How to externalize application.properties files in Spring Boot to e.g. external dependent JAR? Spring-boot - 参数化测试 - 访问MethodSource中的Application.properties - Spring-boot - Parameterized Test - Access Application.properties in MethodSource 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot 在 Spring Boot 中访问 application.properties 文件中定义的值时出现问题 - problem access a value defined in the application.properties file in Spring Boot Spring Boot未加载application.properties - Spring Boot not loading application.properties 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM