This is sample code to assign the value of the property file using the @Value
annotation.
The same result can be obtained with the Configuration class using the @ConfigurationProperties
annotation, which is not covered in this article.
environment
reference
I'm not sure if comma-separated values are called arrays in the properties file, but Spring Boot allows you to assign comma-separated values to collection-type fields.
Property file
properites
hoge.fuga = 100, 200, 300
Alternatively, it can be defined as an array by adding a subscript to the property name, such as [0]
.
properties
hoge.fuga[0]= 100
hoge.fuga[1]= 200
hoge.fuga[2]= 300
In the yml format, define by prefixing the value with -
.
yml
hoge:
fuga:
- 100
- 200
- 300
code
@Value("${hoge.fuga}")
private List<Integer> fuga;
Property file
properites
hoge.fuga =Apple,Mandarin orange,None
yml
hoge:
fuga:
-Apple
-Mandarin orange
-None
code
@Value("${hoge.fuga}")
private List<String> fuga;
Property file
properties
hoge.fuga =Apple,Mandarin orange,None
For yml files, you cannot assign an array to a Set field. If you try to assign an array like the one below, an exception will be thrown.
yml
hoge:
fuga:
-Apple
-Mandarin orange
-None
However, as with the properties file, you can substitute them if they are separated by commas.
yml
hoge:
fuga:Apple,Mandarin orange,None
code
Value("${hoge.fuga}")
private Set<String> fuga;
Property file
properites
hoge.fuga.key1 = val1
hoge.fuga.key2 = val2
yml
hoge:
fuga:
key1: val1
key2: val2
code
@Value("${hoge.fuga}")
private Map<String, String> fuga;
Property file
properties
hoge.fuga.101 = 123456789
hoge.fuga.201 = 456789012
yml
hoge:
fuga:
101: 123456789
201: 456789012
code
@Value("${hoge.fuga}")
private Map<Integer, Long> fuga;
When assigning to LocalDate / LocalDateTime type, implement Formatter as below in the setting class and register it in ConversionService.
@Bean
public ConversionService conversionService() {
Set<FormatterRegistrar> registrars = new HashSet<>();
registrars.add(dateTimeFormatterRegistrar());
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
factory.setFormatterRegistrars(registrars);
factory.afterPropertiesSet();
return factory.getObject();
}
private FormatterRegistrar dateTimeFormatterRegistrar() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeFormatter(dateTimeFormatterFactory());
registrar.setUseIsoFormat(true);
return registrar;
}
//Formatter implementation
private DateTimeFormatter dateTimeFormatterFactory() {
DateTimeFormatterFactoryBean factory = new DateTimeFormatterFactoryBean();
factory.setPattern("yyyy-MM-dd HH:mm:ss");
factory.setTimeZone(TimeZone.getDefault());
factory.afterPropertiesSet();
return factory.getObject();
}
Property file
properites
hoge.fuga = 2017-08-01 23:59:59
yml
hoge:
fuga: 2017-08-01 23:59:59
code
@Value("${hoge.fuga}")
private LocalDateTime fuga;
Property file
properites
hoge.fuga = 2017-08-01
yml
hoge:
fuga: 2017-08-01
code
@Value("${hoge.fuga}")
private LocalDate fuga;
No need to implement a converter or the like. Absolute paths and relative paths are given as samples, but there is no difference in their handling.
Property file
For property files, you will get an error if you do not escape the path delimiter. (In the case of windows environment, Unix / Linux environment is unconfirmed)
properites
hoge.fuga = dir1\\fuga.txt
For yml, you don't need to escape the path delimiter.
yml
hoge:
fuga: dir1\fuga.txt
code
@Value("${hoge.fuga}")
private Path fuga;
Property file
properites
hoge.fuga = D:\\dir1\\dir2\\fuga.txt
yml
hoge:
fuga: D:\dir1\dir2\fuga.txt
code
@Value("${hoge.fuga}")
private Path path;
This is a sample to assign the property value to the enum type.
Resolve and substitute the corresponding enum from the character string described in the property file. No need to implement a converter or the like.
Property file
properites
hoge.fuga = Gold
yml
hoge:
fuga: Gold
** enum definition **
enum
public enum Material {
Bronze,
Sliver,
Gold
;
}
code
Value("${hoge.fuga}")
private Material fuga;
Resolve and substitute the corresponding enum from the field value of the enumerator described in the property file (field called label in this example). In this case, implement the following Converter in the setting class and register it in ConversionService.
@Bean
public ConversionService conversionService() {
Set<Converter<?, ?>> converters = new HashSet<>();
converters.add(new StringToMaterialConverter());
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
factory.setConverters(converters);
factory.afterPropertiesSet();
return factory.getObject();
}
import com.example.lib.constants.Material;
import org.springframework.core.convert.converter.Converter;
public class StringToMaterialConverter implements Converter<String, Material> {
@Override
public Material convert(String value) {
return Material.lookup(value);
}
}
Property file
properites
hoge.fuga =Money
yml
hoge:
fuga:Money
** enum definition **
enum
public enum Material {
Bronze("copper"),
Sliver("Silver"),
Gold("Money")
;
Material(String label) {
this.label = label;
}
private String label;
public String getLabel() {
return this.label;
}
public static Material lookup(String label) {
return Arrays.stream(Material.values())
.filter(material -> material.getLabel().equals(label))
.findFirst().orElseThrow(() -> new RuntimeException("unknown label : " + label));
}
}
code
Value("${hoge.fuga}")
private Material fuga;
Recommended Posts