SpringBoot bottom annotation @ConfigurationProperties configuration binding

1. Prepare the configuration file and configure it in the configuration file (application.properties/yaml/yml)

mycar.te=tesila
mycar.num=888888

Second, the first way: use @Component + @ConfigurationProperties on the entity class to introduce the configuration in the configuration file

@Data
@NoArgsConstructor
@AllArgsConstructor
// 第一种方式: @Component + @ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
    private String te;
    private String num;
}

Third, the second way: add @EnableConfigurationProperties to the configuration class

effect:

1. Turn on the configuration binding function of Car

2. Automatically register the component of this Car entity into the container

@Configuration(proxyBeanMethods = false) // 这是一个配置类 == 配置文件
@EnableConfigurationProperties(Car.class) // 开启属性配置功能, 括号指定绑定的对象
public class MyConfig {
    // ...
}

Test the controller class

@GetMapping("/demo")
public Car demo() {
    return car;
}

Lei Fengyang 2021 version of SpringBoot2 zero-based entry springboot full set of full version (spring boot2)

 

Guess you like

Origin blog.csdn.net/qq_30398499/article/details/113715488