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
Newsletter
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

  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • How an AI Software Factory Works: From Developer Intent to Production
  • Avoiding AI Debt as a Team

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 04, 20 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
21.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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