HubFirms

HubFirms : Blog -How to load external property files into Spring Boot application?

How to load external property files into Spring Boot application?

How to load external property files into Spring Boot application?

It is a standard practice that during our generation arrangements, our application loads property records from outside areas. This encourages us to change our designs without changing our code. In this page, we will come to realize how to stack outside property records into Spring Boot application. 

As a matter of course, Spring Boot search for externalized default property document application.properties into given beneath foreordained areas: 

- In the classpath root. 

- In the bundle "/config" in classpath. 

- In the present catalog. 

- In the "/config" catalog of current organizer. 

Presently lets state, your application requires externalized properties like application.properties and another property document myapp.properties. The two properties can be in a similar organizer or they can be in various envelope. There are 3 different ways of doing it. 

Command line arguments 

In the principal approach, you should simply pass envelope names and property names as a major aspect of order line contentions as demonstrated as follows: 

Terminal 

java - container myapp.jar - spring.config.name=application,myapp 

- spring.config.location=classpath:/information/myapp/config,classpath:/information/myapp/outside/config

In the above order, we are passing property document name as a component of "- - spring.config.name" variable and envelope area as a major aspect of "- - spring.config.location" variable. 

Environment variables 

In the subsequent methodology, you can arrange your externalized setup subtleties into condition factors and your Spring Boot application will peruse it from your condition as demonstrated as follows:

Terminal 

set SPRING_CONFIG_NAME=application,myapp 

set SPRING_CONFIG_LOCATION=classpath:/information/myapp/config,classpath:/information/myapp/outer/config 

java - container myapp.jar

Programatically loding configurations
 

Java vs Python – What’s better for your project?

SpringBootWebApplication
 

package com.hubfirms.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://hubfirms.com
5 Different Ways to Create Objects in Java

 

Author Biography.

Hub Firms
Hub Firms

HubFirms is one of the world’s largest online publications that delivers an international perspective on the latest news about Internet technology, business and culture.

Related Posts