使用ConfigurationProperties代替Value

配置项比较多时,可使用ConfigurationProperties代替Value,使用的方法如下。

1.创建ConfigBean

@ConfigurationProperties(prefix = ConfigBean.PREFIX)
@Data
public class ConfigBean {

    public static final String PREFIX = "test";
    private String abc;
}

2.让Spring扫描

@Bean
public ConfigBean getConfigBean(){
  return  new ConfigBean();
}

3.使用@EnableConfigurationProperties注解开启ConfigurationProperties

@SpringBootApplication
@EnableEurekaClient
@EnableConfigurationProperties
public class SearchApp {
    public static void main(String[] args) {
        SpringApplication.run(SearchApp.class,args);
    }
}

4.注入

@Autowired
ConfigBean config;
posted @ 2021-03-03 00:05  划破黑夜  阅读(290)  评论(0编辑  收藏  举报