application.yml配置文件属性自动装配到实体类中各种数据类型写法
程序员文章站
2022-04-15 23:16:23
application.yml,配置实体类属性apps: userName: yexiang userAge: 27 happy: true birth: 1993/01/04# map: {k1: k1, k2: k2} 简便写法 map: k1: k1 k2: k2# list: [code, music ,dog] 简便写法 list: - code - music - dog dog: name: 来福...
application.yml,配置实体类属性
apps:
userName: yexiang
userAge: 27
happy: true
birth: 1993/01/04
# map: {k1: k1, k2: k2} 简便写法
map:
k1: k1
k2: k2
# list: [code, music ,dog] 简便写法
list:
- code
- music
- dog
# dogName:该字段如果为空,则name用默认值来福,否则取该字段的值
dog:
name: ${apps.dogName:来福}
age: 2
编写实体类
@Data
@Component
@ConfigurationProperties(prefix = "apps")
public class User {
private String userName;
private Integer userAge;
private Boolean happy;
private Date birth;
private Map<String, Object> map;
private List<Object> list;
private Dog dog;
@Data
public static class Dog {
private String name;
private Integer age;
}
此时实体类的Bean已被添加到容器中 ,注入后即可使用
/**
* @author 噎翔
* @date 2020/7/17 23:22
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class Demo {
@Autowired
private User user;
@Test
public void test(){
System.out.println(user.getUserName());
System.out.println(user.getUserAge());
System.out.println(user.getHappy());
System.out.println(user.getBirth());
System.out.println(user.getMap());
System.out.println(user.getList());
System.out.println(user.getDog());
}
}
打印结果:
对应关系:
本文地址:https://blog.csdn.net/weixin_46009162/article/details/107420368