Spring Boot

SpringBoot application startup process analysis

Yaml basic use

# Space requirements are very high!
# Inject into our configuration class!
name: millet
# object
student:
  name: millet
  age:3
# inline
student: {Name: small buried.age: 3}
# array
pets:
  - cat
  - dog
  - pig
pets: [cat.dog.pig]
Copy the code

Assign a sample value to an object using YAML

  • pojo
@ConfigurationProperties(prefix = "people")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class People {
    private String name;
    private int age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;

}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component// Declare it bean
@ConfigurationProperties(prefix = "people.dog")// Scan for elements in yamL files
public class Dog {
    private String name;
    private int age;
    @Bean// Set the bean id to dog01
    public Dog dog01(a){
        return newDog(); }}Copy the code

yaml

people:
  name: Small buried
  age: 8
  happy: true
  birth: 2020/ 5/8
  maps: {K1: V1.K2: V2}
  lists: [tea.milk.The bread]
  dog:
    name: terminal
    age: 78
Copy the code

  • test
@SpringBootTest
class BootConfigApplicationTests {
    @Resource(name = "dog01")
    private Dog dog;
    @Autowired
    private People people;
    @Test
    void contextLoads(a) { System.out.println(people); System.out.println(dog); }}Copy the code

This section annotations

@configurationProperties (prefix = "corresponding prefix in YAML file ")

  • Scanning yamL files (application.yaml)

JSR-303

Generally used to constrain the value of an attribute in an object, its format, whether it can be null, and so on

Bug collection