JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to load external property files into Spring Boot application?


It is a standard practice that during our production deployments, our application loads property files from external locations. This helps us to change our configurations without changing our code. In this page, we will come to know how to load external property files into Spring Boot application.

By default, Spring Boot look for externalized default property file application.properties into given below predetermined locations:

 -- In the classpath root.

 -- In the package "/config" in classpath.

 -- In the current directory.

 -- In the "/config" directory of current folder.

Now lets say, your application requires externalized properties like application.properties and another property file myapp.properties. The both properties can be in the same folder or they can be in different folder. There are 3 ways of doing it.

Command line arguments

In the first approach, all you need to do is pass folder names and property names as part of command line arguments as shown below:

Terminal
java -jar myapp.jar --spring.config.name=application,myapp
--spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config

In the above command, we are passing property file name as part of "--spring.config.name" variable and folder location as part of "--spring.config.location" variable.

Environment variables

In the second approach, you can configure your externalized configuration details into environment variables and your Spring Boot application will read it from your environment as shown below:

Terminal
set SPRING_CONFIG_NAME=application,myapp

set SPRING_CONFIG_LOCATION=classpath:/data/myapp/config,classpath:/data/myapp/external/config

java -jar myapp.jar

Programatically loding configurations


SpringBootWebApplication
package com.java2novice.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

@SpringBootApplication
public class SpringBootWebApplication {

	private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);

	public static void main(String[] args) throws Exception {

		ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootWebApplication.class)
				.properties("spring.config.name:application,myapp",
						"spring.config.location:classpath:/data/myapp/config,classpath:/data/myapp/external/config")
				.build().run(args);

		ConfigurableEnvironment environment = applicationContext.getEnvironment();

		logger.info(environment.getProperty("cmdb.resource-url"));
	}
}

Output
2017-11-22 22:44:24.775  INFO 29696 --- [           main] c.j.springboot.SpringBootWebApplication  : http://java2novice.com
<< Previous Program | Next Program >>

Spring-Boot Examples

  1. Spring-Boot initial setup.
  2. Spring-Boot hello world example
  3. What is spring-boot-starter-parent in Spring-Boot pom.xml file?
  4. What is @SpringBootApplication annotation in spring boot?
  5. What is application.properties in spring boot?
  6. What is @ConfigurationProperties annotation in spring boot?
  7. Spring Boot @ConfigurationProperties example
  8. Spring Boot @ConfigurationProperties Property Validation
  9. Difference between @ConfigurationProperties and @Value
  10. Spring boot web application configurations.
  11. How to run spring boot application through command line?
  12. How to run spring boot as a standalone application (non-web)?
  13. Spring boot property resolution order.
  14. Spring Boot – Profile based properties example.
  15. How to configure logback (SLF4J) logging to spring boot applications?
  16. How to update application context path in spring boot?
  17. How to disable spring logo banner in spring boot?
  18. Spring Data JPA with Spring Boot Applications - Oracle - example
  19. Spring Data JPA with Spring Boot Applications - MySql example
  20. How to configure Spring Boot to show Hibernate SQL Query in logs?
  21. Spring Boot – List all Beans loaded in the ApplicationContext
  22. How to load external property files into Spring Boot application?
  23. How to rename application.properties file in Spring Boot application?
  24. How to configure multiple DataSources (Databases) with Spring Boot and Spring Data?
Knowledge Centre
What is OOPs?
Object oriented programming organizes a program around its data, i.e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.