@ConfigurationProperties
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties
属性名匹配规则:
自动注入
配置文件对应的值与对象的属性一一对应
spEl表达式
不支持
校验
对lastname使用邮箱校验
@Validated//校验使用,@configurationProperties比较用的
public class Person {
@Email//邮箱格式的校验方式
private String lastname;
发现错误:
@value
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;
属性名匹配规则:
需要与原来的对应
自动注入
对象的每一个属性都需要手动添加@value进行赋值
spEl
支持使用表达式,值得赋值方式可以为以下几种:
举例:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ValueController {
@Value("#{person.lastname}")
private String name;
@RequestMapping("/helloqq")
public String Heollo()
{
return "hello"+name;
}
}
启动服务器:
扫描二维码关注公众号,回复:
11977945 查看本文章