In the development of using SpringBoot, some configuration parameters need to be defined in the yml file, and then introduced through Java classes
Spring boot provides some annotations to implement this function
- ConfigurationProperties
- Value
- EnableConfigurationProperties
The following provides examples to show how to introduce general variables, arrays, lists, m ap s, and reference objects.
Introducing pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
<!--lombok Plug in, not required -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
Annotation
A global annotation class is defined here,
@Data @ToString @ConfigurationProperties(prefix = "all") public class AllConfigurationProperties {
//Ordinary variable private String name;
//Reference object private OtherProperties other; //array private String[] server; //list private List list; //map private Map map; //Complex map private Map<String, ModuleConfig> modules = new LinkedHashMap(); //Complex list private List<ModuleConfig> modulesList; }
ConfigurationProperties: the specifier is a configuration class, which requires prefix configuration in yml.
What should be paid attention to
1. The name in the configuration class should conform to the naming method of Java Bean
2. The name in the configuration class should be the same as that in yml, otherwise @ Value should be used to specify
For example:
yml Document: all: name: libai ------------------------------------
@Value("${all.name}") private String myName;
3. If @ Value mode has been used, Setter method is not needed. Otherwise, the Setter method must be written for this variable. Here, use the annotation @ Data of lombok to configure. The Setter, getter and toString methods will be generated automatically
Reference object of the above configuration class
@Data public class ModuleConfig { private static final long serialVersionUID = 5508512956753757169L; private String name; private String version; private String owner; } @Data public class OtherProperties { private Long id; private String version; }
Enable configuration class
@Slf4j @EnableConfigurationProperties(AllConfigurationProperties.class) @Configuration public class AutoConfiguration { @Autowired AllConfigurationProperties properties; @PostConstruct public void init(){ System.out.println("properties = " + properties); } }
Use the @ EnableConfigurationProperties enable configuration class here. It will inject configuration parameters in yml for AllConfigurationProperties and create a bean, which can be used later by @ Autowired injection
@Configuration indicates that this is a Spring b oot configuration class
Use the init() method to output the configuration
Configuration in yml
all:
name: libai
other:
id: 100
version: 1.0.1
server:
- 127.0.0.1
- 127.0.0.2
- 127.0.0.3
list:
- 111
- 222
- 333
map:
key1: value1
key2: value2
key3: value3
modules:
key1:
name: modules-name-1
version: modules-version-1
owner: modules-owner-1
key2:
name: modules-name-2
version: modules-version-2
owner: modules-owner-2
modulesList:
- name: modules-name-3
version: modules-version-3
owner: modules-owner-3
- name: modules-name-4
version: modules-version-4
owner: modules-owner-4
output
properties =
AllConfigurationProperties(
name=libai,
other=OtherProperties(id=100, version=1.0.1),
server=[127.0.0.1, 127.0.0.2, 127.0.0.3],
list=[111, 222, 333],
map={
key1=value1,
key2=value2,
key3=value3
},
modules={
key1=ModuleConfig(name=modules-name-1, version=modules-version-1, owner=modules-owner-1),
key2=ModuleConfig(name=modules-name-2, version=modules-version-2, owner=modules-owner-2)
},
modulesList=[
ModuleConfig(name=modules-name-3, version=modules-version-3, owner=modules-owner-3),
ModuleConfig(name=modules-name-4, version=modules-version-4, owner=modules-owner-4)
])