1. Introduction
In Spring Boot projects we often need to read custom configurations from application.yml configuration files. Today we will list some common methods and methods to read configuration files from YAML.
2. Use @value annotations
First, think of the @Value annotation, which can only parse simple types in YAML files and bind them to object properties.
felord:
phone: 182* * * * * * 32
def:
name: Small fat brother
blog: felord.cn
we-chat: MSW_623
dev:
name: Small fat brother
blog: felord.cn
we-chat: MSW_623
type: JUEJIN
Copy the code
For the yamL configuration above, if we use the @value annotation, the key directly following the colon will inject the corresponding Value correctly. For example, we can get felord.phone with @value, but we can’t get felord.def because there is no direct Value after felord.def, and it has a lower level option. In addition, @value does not support yamL loose binding syntax, that is, felord.def.weChat does not obtain the Value of felord.def.we-chat.
@value is obtained by using Spring’s SpEL expression:
// Get the value of felord.phone in yaml and provide the default value UNKNOWN
@Value("${felord.phone:UNKNOWN}")
private String phone;
Copy the code
@value is used when only one of the values in the configuration file is needed. If we need to inject a series of values by binding, it is recommended to use the form of complex object injection.
3. Use the @ConfigurationProperties annotation
The @ConfigurationProperties annotation provides the ability to inject multiple configuration options into complex objects. It requires us to specify a common prefix for the configuration. For example, we want to bind all configuration items under felord.def:
package cn.felord.yaml.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import static cn.felord.yaml.properties.FelordDefProperties.PREFIX;
/ * * *@author felord.cn
*/
@Data
@ConfigurationProperties(PREFIX)
public class FelordDefProperties {
static final String PREFIX = "felord.def";
private String name;
private String blog;
private String weChat;
}
Copy the code
We noticed that we could receive we-chat values using weChat because this form supports automatic conversion from camel camel-case to short horizontal name kebab-case.
“Properties” is recommended for @ConfigurationProperties, such as “RedisProperties” for Redis and “RabbitProperties” for RabbitMQ.
In addition if we want to @ NestedConfigurationProperty notes can help realize nested. Inner classes are also available. This uses the inner class implementation to inject all the attributes in the initial YAML:
package cn.felord.yaml.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import static cn.felord.yaml.properties.FelordProperties.PREFIX;
/** * Internal class and enumeration configuration. **@author felord.cn
*/
@Data
@ConfigurationProperties(PREFIX)
public class FelordProperties {
static final String PREFIX = "felord";
private Def def;
private Dev dev;
private Type type;
@Data
public static class Def {
private String name;
private String blog;
private String weChat;
}
@Data
public static class Dev {
private String name;
private String blog;
private String weChat;
}
public enum Type {
JUEJIN,
SF,
OSC,
CSDN
}
}
Copy the code
Using @ConfigurationProperties alone still makes it impossible to directly use the configuration object FelordDefProperties because it is not registered as a Spring Bean. We can make it work in two ways.
3.1 Explicitly inject Spring IoC
You can inject FelordDefProperties into Spring IoC using @Component, @Configuration, etc.
package cn.felord.yaml.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import static cn.felord.yaml.properties.FelordDefProperties.PREFIX;
/** * Explicitly inject Spring IoC *@author felord.cn
*/
@Data
@Component
@ConfigurationProperties(PREFIX)
public class FelordDefProperties {
static final String PREFIX = "felord.def";
private String name;
private String blog;
private String weChat;
}
Copy the code
3.2 use @ EnableConfigurationProperties register
We can also use annotations @ EnableConfigurationProperties register, so there is no need to explicitly declared configuration class for Spring beans.
package cn.felord.yaml.configuration;
import cn.felord.yaml.properties.FelordDevProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/** * use {@linkRegistered EnableConfigurationProperties} {@linkFelordDevProperties} make it effective *@author felord.cn
*/
@EnableConfigurationProperties({FelordDevProperties.class})
@Configuration
public class FelordConfiguration {}Copy the code
This annotation requires an explicit registration of the corresponding configuration class.
3.3 using the @ ConfigurationPropertiesScan scan
In the Spring the Boot 2.2.0. RELEASE the annotation @ ConfigurationPropertiesScan provides a scan. It can scan all the configuration classes flagged by @ConfigurationProperties under a particular package and inject them IoC.
package cn.felord.yaml;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
/ * * * {@linkConfigurationPropertiesScan} and {@linkAlternative EnableConfigurationProperties} * *@see cn.felord.yaml.configuration.FelordConfiguration
* @author felord.cn
*/
@ConfigurationPropertiesScan
@SpringBootApplication
public class SpringBootYamlApplication {
public static void main(String[] args) { SpringApplication.run(SpringBootYamlApplication.class, args); }}Copy the code
This is ideal for automatic injection and batch injection of configuration classes, but is versioned and must be 2.2.0 or higher.
3.4 the Environment
Spring the Boot program can also pass org. Springframework. Core. The env. The Environment provided by the getProperty (String key) to obtain, general is not very common.
4. To summarize
@Value is recommended for a single property in daily development, and @ConfigurationProperties is recommended for multiple properties. It should be added that @ConfigurationProperties also supports property validation using JSR303. More attention: code farmer Xiao Pang gets more technical dry goods. Relevant demos can be obtained by replying to YAML through the public account.
Follow our public id: Felordcn for more information
Personal blog: https://felord.cn