DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Web Service Using Spring Boot 2.x
  • How to Activate New User Accounts by Email
  • How To Read the Properties File Outside the Jar in Spring Boot
  • Bing Maps With Angular in a Spring Boot Application

Trending

  • Amazon Quick: AWS's Agentic Workspace, Explained for Engineers
  • A Spring Boot App With Half the Startup Time
  • Skills, Java 17, and Theme Accents
  • Is the Data Warehouse Dead? 3 Patterns From Enterprise Architecture That Answer This Question
  1. DZone
  2. Coding
  3. Frameworks
  4. Externalize Property File, Traditional War Deployment in Spring Boot

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.

By 
Rohit upadhyay user avatar
Rohit upadhyay
·
May. 04, 20 · Analysis
Likes (3)
Comment
Save
Tweet
Share
21.3K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Framework Spring Boot File system Property (programming) WAR (file format)

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

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • How to Activate New User Accounts by Email
  • How To Read the Properties File Outside the Jar in Spring Boot
  • Bing Maps With Angular in a Spring Boot Application

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook