Externalize Property File, Traditional War Deployment in Spring Boot
Sometimes we have to configure property files from outside of classpath so that anyone can access this file without looking into application.
Join the DZone community and get the full member experience.
Join For FreeSometimes we have to configure property files from outside of classpath so that anyone can access this file without looking into application. Spring boot provides many ways to configure this but most of them are used only if we read the property file from classpath if we are going to read from file system then it will not accessible and will get some error so now we are going to see how to overcome from this problem.
Spring-Boot Default Search for Property File Is
- /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
This is the standard order.
To externalize property file we have to override configure the location of application by changing the property of spring.config.location.
If we use spring.config.location property then it will replace default location of property file and profile setup also. we can provide additional locations also which will be searched before the default locations.
x
public class Application extends SpringBootServletInitializer {
// set spring.config.location here if we want to run the application as a jar
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.sources(Application.class)
.properties(getProperties())
.run(args);
}
// set spring.config.location here if we want to deploy the application as a war on tomcat
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
return springApplicationBuilder
.sources(Application.class)
.properties(getProperties());
}
static Properties getProperties() {
Properties props = new Properties();
props.put("spring.config.location","file:///D:/file_dir/application.properties");
return props;
}
}
Conclusion
In this post, I explained how you can run your Spring Boot application on external Tomcat. For me, this was a real-world situation where I had to solve this query.
Hopefully, this will provide some useful knowledge when you will face a similar problem. If you like my post, do not forget to share!
Lastly, for deeper understanding, I suggest you read the official Spring documentation here.
As we can see, the Spring Boot framework itself takes care of externalized configuration for us.
Often, we just have to place the property values in the correct files and locations, but we can also use Spring’s Java API for more control.
Thanks for giving your valuable time. Keep reading and keep learning.
Published at DZone with permission of Rohit upadhyay. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments