每天一个SpringBoot注解之@ConfigurationProperties

博客介绍了Spring Boot项目读取配置的方法,重点讲解了使用@ConfigurationProperties注解读取配置。该注解可放在类或方法上,通过指定前缀绑定配置文件中的配置。还举例说明了其在类和方法上的用法,并对比了与@value的功能,指出其写法方便但对象类命名有要求。

我们知道,每一个springboot项目都会有很多配置,application.yml,bootstrap.xml等。数据库连接地址,redis地址,nacos,dubbo,cloud等,而我们读取这些配置,因为spring的版本进行了几次更新。
1.xml配置,通过xml去获取bean
2.注解,我们通常使用@Value等注解去获取配置
3.java Config,java Config是spring的一个子项目,我们通过java代码和@ConfigurationProperties注解去读取配置。
今天我说的就是第三种方法,我们点开这个注解,常用的属性就是prefix,通过指定的前缀,绑定配置文件中的配置,该注解可以放在类上,也可以放在方法上

/**
 * Annotation for externalized configuration. Add this to a class definition or a
 * {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate
 * some external Properties (e.g. from a .properties file).
 * <p>
 * Note that contrary to {@code @Value}, SpEL expressions are not evaluated since property
 * values are externalized.
 *
 * @author Dave Syer
 * @since 1.0.0
 * @see ConfigurationPropertiesBindingPostProcessor
 * @see EnableConfigurationProperties
 */
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ConfigurationPrope
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值