Sometimes 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.
@SpringBootApplication 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
@Override 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.
InnovationM is a globally renowned Mobile app development company in India that caters to a strong & secure Android app development, iOS app development, hybrid app development services. Our commitment & engagement towards our target gives us brighter in the world of technology and has led us to establish success stories consecutively which makes us the best iOS app development company in India.
Thanks for giving your valuable time. Keep reading and keep learning.