一:通过yml 给对象赋值 -->推荐
通过@ConfigurationProperties注解声明当前类为属性读取类
prefix="person"读取属性文件中,前缀为person的值。
1.创建Person类
@Data
@Component
@ConfigurationProperties(prefix = "person") //给这个类加一个前缀,在yaml中才能识别
public class Person {
private String name;
private Integer age;
private Date birthday;
private List<Object> lists;
private Dog dog;
private Map<String,Object> maps;
}
2.创建Dog类
@Component
@Data
public class Dog {
@Value("14")
private int age;
private String name;
}
3.编写yml文件
person:
age: 18
name: 夏木
birthday: 2020/05/22
lists:
- one
- two
- three
maps: {hubei: 武汉,zheJiang: 杭州}
dog:
age: 5
name: 大黄
测试:
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
输出:
Person(name=夏木, age=18, birthday=Fri May 22 00:00:00 CST 2020, lists=[one, two, three], dog=Dog{age=5, name='大黄'}, maps={hubei=武汉, zheJiang=杭州})
2.properties文件
通过我们的@ConfigurationProperties和@PropertySource配合使用
@Data
@Component
@ConfigurationProperties("data.person")
@PropertySource(value = "classpath:person.properties")
public class Person {
private String name;
private Integer age;
private Date birthday;
private List<Object> lists;
private Dog dog;
private Map<String,Object> maps;
}
输出:
Person(name=ÏÄľ, age=19, birthday=Fri May 22 00:00:00 CST 2020, lists=[a, b, c], dog=Dog(age=3, name=½ðë), maps={k1=ÉϺ£})
我们发现中文输出是乱码 ,打开我们的编辑器,更改我们的编码就可以了
3.@Value
这样使用不太友好,需要给每个属性单独赋值,比较麻烦
@Data
@Component//注册bean
public class Person {
@Value("${person.name}") //从配置文件中读
private String name;
@Value("#{10*2}") //#{SPEL} spring表达式
private Integer age;
@Value("2020/5/22") //普通字面量
private Date birthday;
private List<Object> lists;
private Dog dog;
private Map<String,Object> maps;
}
/ | @ConfigurationProperties | @Value |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEl) | 不支持 | 支持 |
jSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
- cp只需要写一次即可, value则需要每个字段都添加
- 松散绑定:这个什么意思呢?比如我的yml中写的first-name,这个和firstName是一样的,-后面跟着的字母默认是大写的。这就是松散绑定
- JSR303数据校验,这个就是我们可以在字段是增加一层过滤器验证,可以保证数据的合法性复杂类型封装,yml中可以封装对象,使用@value就不支持
松散绑定:
@Component
@Data
@ConfigurationProperties(prefix = "dog")
public class Dog {
private int age;
private String firstName;
}
dog:
age: 5
first-name: 大黄
扩展JSR303校验:
JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。
Hibernate Validator 附加的 constraint
constraint | 信息 |
---|---|
被注释的元素必须是电子邮箱 | |
@Length | 被注释的元素必须在指定范围长度内 |
@NotEmpty | 被注释的元素不能为空 |
接下来我们就来以@Email为例
@Data
@Component
@Validated //开启数据校验
@ConfigurationProperties(prefix = "person")
public class Person {
@NotNull(message = "姓名不能为空") //自定义校验信息
private String name;
private Integer age;
}
异常信息:
rejected value [null]; codes [NotNull.name,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [.name,name]; arguments []; default message [name]]; default message [姓名不能为空]
当然 我们在yml配置文件上写上给name属性注值就好了
当然 这些注解我们想用哪个就用哪个
以上就是SpringBoot 给属性赋值的方式 和JSR303校验,当然属性赋值方式推荐使用yml了