In Spring Boot, we have summarized the binding method with the property file.
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
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,...
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
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
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);
}
}
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