How to bind to property file in Spring Boot

In Spring Boot, we have summarized the binding method with the property file.

Single item banting

Bind the following properties to Java fields.

application.properties


foo.intervel=1000
bar.intervel=500

First, create a context class by specifying the playfix with @ConfigurationProperties for each playfix (foo, bar).

@ConfigurationProperties(prefix="foo")
public class FooContext {
	
	private int intervel;
	
	public int getIntervel() {
		return intervel;
	}

	public void setIntervel(int intervel) {
		this.intervel = intervel;
	}
}
@ConfigurationProperties(prefix="bar")
public class BarContext {
	
	private int intervel = 10; //Default value can be specified here
	
	public int getIntervel() {
		return intervel;
	}

	public void setIntervel(int intervel) {
		this.intervel = intervel;
	}
}

After that, you can register the context class in DI.

@Configuration
public class PropertiesConfiguration {
	
	@Bean
	public FooContext fooConfig() {
		return new FooContext();
	}

	@Bean
	public BarContext barConfig() {
		return new BarContext();
	}
}

Get the property value with the following feeling.

	@Autowired
	private FooContext fooContext;

	@Autowired
	private BarContext barContext;
	
	public void test {
		int fooIntervel = fooContext.getIntervel();
		int barIntervel = barContext.getIntervel();
	}

In the case of camel case, you can connect words with hyphens or leave camel as it is.

@ConfigurationProperties(prefix="foo")
public class FooContext {
	
	private int itemName;
	
	// getter & setter
}

application.properties


foo.item-name=apple

application.properties


foo.itemName=apple

Binding with Collection type

For lists and arrays, bind to properties with the following feeling:

@ConfigurationProperties(prefix="foo")
public class FooContext {
	
	private List<String> items;    //or private String[] items;

	// getter & setter
}

application.properties


foo.items[0]=apple
foo.items[1]=banana

You can also write them separated by commas.

application.properties


foo.items=apple,banana,...

Binding with Map type

In the case of Map type, bind to the property with the following feeling.

@ConfigurationProperties(prefix="foo")
public class FooContext {
	
	private Map<String, String> itemMap;
	
	// getter & setter
}

application.properties


foo.item-map.key1=apple
foo.item-map.key2=banana

Nested object binding

In the case of a nested object, bind it to the property with the following feeling.

@ConfigurationProperties(prefix="foo")
public class FooContext {
	
	private Product product;	
	
	// getter & setter
}
public class Product {
	
	private String productId;
	private String productName;
	
	// getter & setter

application.properties


foo.product.product-id=001
foo.product.product-name=apple

The same applies to list + nesting.

@ConfigurationProperties(prefix="foo")
public class FooContext {
	
	private List<Product> products;	
	
	// getter & setter
}

application.properties


foo.products[0].product-id=001
foo.products[0].product-name=apple
foo.products[1].product-id=002
foo.products[1].product-name=banana

Binding to multiple property files in classpath

When specifying multiple property files in the classpath, use @PropertySources.

@SpringBootApplication
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:another.properties")
})
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

External property binding

When using an external property file, specify the file path on the start command line.

java -jar demo.jar --spring.config.location="file:/tmp/properties/application.properties"

Recommended Posts

How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
How to split Spring Boot message file
How to add a classpath in Spring Boot
Procedure to make the value of the property file visible in Spring Boot
How to bind request parameters in list format with bean property in Spring
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to test file upload screen in Spring + Selenium
How to use Lombok in Spring
How to set Spring Boot + PostgreSQL
How to use ModelMapper (Spring boot)
How to set environment variables in the properties file of Spring boot application
How to change application.properties settings at boot time in Spring boot
Extract SQL to property file with jdbcTemplate of spring boot
How to call and use API in Java (Spring Boot)
How to control transactions in Spring Boot without using @Transactional
Local file download memorandum in Spring Boot
How to add jar file in ScalaIDE
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
How to not start Flyway when running unit tests in Spring Boot
How to get the setting value (property value) from the database in Spring Framework
How to define multiple orm.xml in Spring4, JPA2.1
Spring Boot --How to set session timeout time
[Spring Boot] How to get properties dynamically from a string contained in a URL
How to set Dependency Injection (DI) for Spring Boot
How to write a unit test for Spring Boot 2
[Spring Boot] How to create a project (for beginners)
How to convert a file to a byte array in Java
How to debug the generated jar file in Eclipse
How to boot by environment with Spring Boot of Maven
How to use In-Memory Job repository in Spring Batch
Try Spring Boot from 0 to 100.
Spring Boot 2 multi-project in Gradle
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Introduction to Spring Boot ② ~ AOP ~
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Introduction to Spring Boot Part 1
How to implement authentication process by specifying user name and password in Spring Boot
How to correctly check the local HTML file in the browser
How to read your own YAML file (*****. Yml) in Java
[Rails5] japanMap link How to write parameters in js.erb file
Set up Multipart Resolver to allow file uploads in Spring
Use @ControllerAdvice, @ExceptionHandler, HandlerExceptionResolver in Spring Boot to catch exceptions
I want to control the maximum file size in file upload for each URL in Spring Boot
Let's find out how to receive in Request Body with REST API of Spring Boot
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
How to unit test Spring AOP
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
How to use Spring Data JDBC
[Rails] How to write in Japanese
How to run Ant in Gradle
Write test code in Spring Boot
How to master programming in 3 months