{{announcement.body}}
{{announcement.title}}

Externalize Property File, Traditional War Deployment in Spring Boot

DZone 's Guide to

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.

· Big Data Zone ·
Free Resource

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

  1. /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. 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.

Java
 




x


 
1
@SpringBootApplication
2
public class Application extends SpringBootServletInitializer {
3
// set spring.config.location here if we want to run the application as a jar
4
public static void main(String[] args) {
5
new SpringApplicationBuilder(Application.class)
6
.sources(Application.class)
7
.properties(getProperties())
8
.run(args);
9
}
10
// set spring.config.location here if we want to deploy the application as a war on tomcat
11
 
          
12
@Override
13
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
14
return springApplicationBuilder
15
.sources(Application.class)
16
.properties(getProperties());
17
}
18
static Properties getProperties() {
19
Properties props = new Properties();
20
props.put("spring.config.location","file:///D:/file_dir/application.properties");
21
return props;
22
}
23
}
21
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.

Topics:
big data, java, spring, spring boot, springboot, springbootapplication

Published at DZone with permission of Rohit upadhyay . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}