i have spring boot application.
i am trying to access application.properties file from tomcat location.
i followed this link : 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
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.
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 deploymentNeed to copy the application.properties file to tomcat directory
lib
location. then we need to change theServletInitializer.java
file."classpath:mortgage-api/"
means we need to create a folder with namemortgage-api
in tomcat lib folder and will copy application.properties file to this location. check the code comment.then
mvn clean
, then build war filemvn install
Please add -
or
Use annotation above class -
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html