@ConfigurationProperties绑定属性支持属性名宽松绑定,而@Value不支持宽松绑定。宽松绑定的具体规则为:
绑定前缀名命名规范:仅能使用纯小写字母、数字、下划线作为合法的字符(一般我们都用纯小写字母)
范例:
@Component
@Data
public class DataSoureConfig {
@Bean
@ConfigurationProperties(prefix = "datasource")
public DruidDataSource druidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
}
dataSource:
driverClassName: com.mysql.jdbc.Driver456
范例解释:虽然我们在application.yml中使用了带有大写字母的dataSource配置,但是在配置类中,我们依然使用全小写datasource,不然会报错。