Array Type Valuation in SpringBoot yaml
- 2021-11-10 09:41:48
- OfStack
Directory yaml Array Type Value First the following simple configuration how to value springboot configuration file yml Array Form Configuration File Entity Class
Array Type Values in yaml
The simple style in yaml is popular with everyone for 10 points
First, let's talk about how to take the value of simple configuration
# application-dev.yml
testValue:
testValueChild: testValueChildValue
...
// SomeServiceImpl.java
@Service
public class SomeServiceImpl {
// So you can get the configuration information directly
@Value("${testValue.TestValueChild}")
private String testValueChild;
...
}
Sometimes we will need some array types. Here is a brief introduction to the writing of array configuration information, such as the configuration of the following formats, whether data synchronization is turned on, and the data types that data synchronization needs to be synchronized.
dataSync:
enable: true
type:
- "1"
- "2"
- "3"
You can't use @ Value to take the value at this time, but you can take the value in the following ways.
...
// Separate registration 1 A bean For storing such configuration information
@Component
@Data
@ConfigurationProperties(prefix = "data-sync")
public class DataSyncConfig {
private Boolean enable;
private List<String> types;
}
...
public class SomeServiceImpl{
@AutoWired
private DataSyncConfig dataSyncConfig;
public void youerMethod() {
List<String> types = dataSyncConfig.getTypes();
}
}
Array form of springboot configuration file yml
Configuration file
proxy:
url:
- "http://www.baidu.com"
- "http://www.jd.com"
Entity class
@Data
@NoArgsConstructor
@AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "proxy")
public class ProxyConfig {
private String[] url;
}
The reference name ('url') in the object must match the ('url') 1 in the yml file, otherwise the data will not be retrieved.