| @Value | @ConfigurationProperties | |
|---|---|---|
| 松散绑定(松散语法,userId user-id) | 不支持 | 支持 |
| SpEL | 支持 | 不支持 |
| JSR303数据校验 | 不支持 | 支持 |
| 复杂类型封装 | 不支持 | 支持 |
@ConfigurationProperties(prefix = "defaultuser")
@Component
@Validated
public class TestUser{
@Email
private String email;
}
@Value(“value/${user.age}/#{2*4}”)
@Value注解可以添加三种值类似于Spring的配置文件
(1)字面量(@ConfigurationProperties取得的配置信息会覆盖本注解注入的值)(2)${key}从环境变量或者配置文件中取值(从配置文件中取值类似于@ConfigurationProperties的效果,配置文件优先于环境变量) 3、表达式#{SpEL}例如#{2*4}
@PropertySource(value = {"classpath:test-user.properties"})
@ConfigurationProperties(prefix = "defaultuser")
@Component
@Validated
public class TestUser extends Human {
@Value("${defaultuser.id}")
private Integer id;
}



