@ConfigurationProperties注解解析

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

关于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);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值