YAML syntax can refer to "Basic Grammar of YAML File"
Application.properties and application.yml are SpringBoot configuration files. They are located in the src/main/resources
directory and are used to modify the default configuration.
1. First src/main/resources
create application.yml in the directory
~/Desktop/MySpringboot$ touch src/main/resources/application.yml
2. Add the following configuration information to application.yml
server:
port: 8088
person:
lastName: Sam
age: 18
boss: false
birth: 2020/03/21
maps: {k1: v1,k2: v2}
lists:
- Tom
- Sam
dog:
name: small dog
age: 2
If the application.properties file is used for configuration, then the above data is equivalent to:
server.port=8088
person.lastName=Sam
person.age=18
person.boss=false
person.birth=2020/03/21
person.maps.k1=v1
person.maps.k2=v2
person.lists=Tom,Sam
person.dog.name=small dog
person.dog.age=2
3. Create a Person class to map the attribute values in the configuration file to this class
Person.java
package com.wong.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix = "person":配置文件中哪个下面的所有属性值进行一一映射
* 注意:只有这个组件是容器的组件,才能使用容器提供的ConfigurationProperties功能
*/
@Configuration
@ConfigurationProperties(prefix = "person")
public class Person{
private String lastName;
private int age;
private boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
// 省略setter、getter方法
}
- @ConfigurationProperties: Tell SpringBoot to bind all the properties in this class to the relevant configuration in the configuration file, prefix = "person": which one-to-one mapping of all the property values in the configuration file
- @Configuration is used to define the configuration class. The annotated class contains one or more methods annotated with @Bean. These methods will be scanned by AnnotationConfigApplicationContext or AnnotationConfigWebApplicationContext class, and used to build bean definitions and initialize the Spring container.
- Spring's @Bean annotation is used to tell the method to generate a Bean object, and then this Bean object is handed over to Spring management. The method of generating this Bean object Spring will only be called once, then this Spring will put this Bean object in its own IOC container
In fact, we can also use @Value annotation to bind the value:
@Configuration
//@ConfigurationProperties(prefix = "person")
public class Person{
@Value("${person.lastName}")
private String lastName;
@Value("${person.age}")
private int age;
@Value("${person.boss}")
private boolean boss;
@Value("${person.birth}")
private Date birth;
@Value("${person.maps}") // 失败
private Map<String,Object> maps;
@Value("${person.lists}") // 失败
private List<Object> lists;
@Value("${person.dog}") // 失败
private Dog dog;
// 省略setter、getter方法
}
Compare the difference between @Value and @ConfigurationProperties:
@ConfigurationProperties | @Value | |
---|---|---|
Features | Bulk injection of attributes in configuration files | Specify one by one |
Loose grammar | Support, ie lastName, last_name, LAST_NAME will be considered the same | not support |
Game | not support | Support, see example 1 |
JSR303 data verification | Support, see example 2 | not support |
Complex type packaging | stand by | not support |
Therefore, if there is a complex type encapsulation or a javaBean is defined to map configuration items, we use @ConfigurationProperties. When using an attribute alone, use @Value.
Example 1:
@Configuration
public class Person{
@Value("#{10*2}")
private int age;
// 省略setter、getter方法
}
Example 2:
@Configuration
@ConfigurationProperties(prefix = "person")
@Validated
public class Person{
@mail
private String lastName; // 必须符合邮件格式
...
}
4. Use configuration information
@RestController
@RequestMapping("world")
public class HelloWorldController{
@Autowired
private Person person;
@GetMapping("hi")
public String index(){
return person.getLastName();
}
}
@Autowired is an annotation that can annotate class member variables, methods, and constructors, allowing spring to complete the automatic assembly of beans.
5. Run and test
run:
~/Desktop/MySpringboot$ mvn spring-boot:run
Test in the browser:
6. Unit testing
Create a test class under src / test / java / com / wong:
package com.wong;
import com.wong.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootConfigTests {
@Autowired
private Person person;
@Test
public void testPerson(){
System.out.println(person.getLastName()+"kkkkk");
}
}
Right-click SpringBootConfigTests.java and click RUN 'SpringBootConfigTests' to run the test class: the
test passed. thanks for reading!