@ConfigurationProperties、@EnableConfigurationProperties等等注解使用及区别


@ConfigurationProperties(prefix = "a.b") 

  1、当类上只有上方注解时,此类是没有加入到Spring容器中的,需要结合@Compent等注解一起使用,才会与配置文件一起联动

  2、但还有一种情况,就是如果类上只有上方注解,但可结合下方注解所在的类一起使用,也会注入spring容器中

  使用上方注解会警告 需要导入一个pom


  org.springframework.boot
  spring-boot-configuration-processor
  true

@EnableConfigurationProperties

  1.使用此注解需要 @Configuration 不然一样不注入Spring容器中,和上述使用方法是类似的

@Configuration
@EnableConfigurationProperties(value = Config.class)
public class AConfig {
    @Autowired
    private Config config;
}
//@Component @ConfigurationProperties(value = "a.b") public class Config { String name; Integer age; public String getName() {return name;} public void setName(String name) {this.name = name;} public Integer getAge() {return age;} public void setAge(Integer age) {this.age = age;} }