关于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);
}
}
SpringBoot的@EnableConfigurationProperties注解用于启用@ConfigurationProperties注解的Bean,将配置文件中的属性值绑定到对应的Java对象上。例如,RegistrationTypeProperties类通过@ConfigurationProperties(prefix = config)注解,将配置文件中config前缀的属性值映射到类的属性上。在启动类中使用@EnableConfigurationProperties(RegistrationTypeProperties.class)使配置生效,使得我们可以在应用中直接注入并使用这些配置属性。
2813

被折叠的 条评论
为什么被折叠?



