@ConfigurationProperties注解解析

关于EnableConfigurationProperties,在SpringBoot的注释中是这样说明的:为带有@ConfigurationProperties注解的Bean提供有效的支持。

@EnableConfigurationProperties的作用是把springboot配置文件中的值与我们的RegistrationTypeProperties .java的属性进行绑定,需要配合@ConfigurationProperties使用 (需要加@Component)

ConfigurationProperties

@Component
@ConfigurationProperties(prefix = "config")
@RefreshScope
@Data
public class RegistrationTypeProperties {

    private Integer xyType;

    private Integer pushType;

}

@ConfigurationProperties 注解,通常是将properties和yml文件转换成bean对象;

以上步骤可以将配置文件属性注册到IOC容器中,在获取这些bean之前,首先需要使用@EnableConfigurationProperties({ConfigBean.class})  注解使ConfigurationProperties注解生效:

@MapperScan("com.capinfo.*.mapper")
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableConfigurationProperties({
        RegistrationTypeProperties.class
})
public class PerceptionApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(PerceptionApplication.class, args);
    }
}


版权声明:本文为SYT_Boss原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。