access externalize application.properties in Tomcat for Spring boot application?

試著忘記壹切 提交于 2019-12-04 20:16:25

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

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

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>

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

"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;
       }

}

then mvn clean, then build war file mvn install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!