spring-bootwarweb-inf

Spring Boot - deploy .properties file to a folder different than 'WEB-INF/classes'


I'm trying to convert a traditional Tomcat Spring MVC webapp to Spring Boot. The new application should still use .war deployment.

For various reasons I have the obligatory requirement that the application.properties file resides inside a WEB-INF/conf folder in the deployed app and NOT inside the WEB-INF/classes folder where Spring Boot puts it by default.

In the original webapp I could put the application.properties file inside the src/main/webapp/WEB-INF/conf folder (so they get copied to WEB-INF/conf in the deployed application) and then use it like this:

<context:property-placeholder location="/WEB-INF/conf/application.properties"/>

What is the Spring Boot way to refer to this location?

I tried adding each of the following:

spring.config.location=WEB-INF/conf/application.properties

but my application.properties file still doesn't get loaded.


Solution

  • What finally worked was the following @PropertySource annotation.

    @SpringBootApplication
    @PropertySource(value = {"WEB-INF/conf/application.properties"})
    public class MyApplication {
            public static void main(String[] args) {
            SpringApplication.run(MyApplication.class, args);
        }
    }
    

    It seems that not specifying classpath: or file: at the beginning of a path makes it use a path relative to the webapp.

    I'm still not sure as to why specifying

    spring.config.location=WEB-INF/conf/application.properties
    

    didn't have the same effect.