赞
踩
application.properties
以配置端口和访问路径为例
-
- server.port=8080
application.yml / application.yaml
- server:
- port: 81
在实际开发中,更常用的是yaml配置文件
yaml层级表示更加明显
- lesson: SpringBoot
-
- enterprise:
- name: itcat
- age: 16
- tel: 4000161933
- subject:
- - java
- - 前端
- - 大数据
yml书写注意事项:
值前边必须有空格,作为分隔符
使用空格作为缩进表示层级关系,相同的层级左对齐
- //yaml文件数据读取1
- @Value("${lesson}")
- private String lesson;
-
- @Value("${enterprise.subject[0]}")
- private String subject_00;
- //yaml文件数据读取2
- @Autowired
- private Environment environment;
- //yaml文件数据读取3
- @Autowired
- private Enterprise enterprise;
方式三实体类如下
- //封装yaml对象格式数据必须先声明当前实体类受Spring管控
- @Component
- //使用@ConfigurationProperties注解定义当前实体类读取配置属性信息,通过prefix属性设置读取哪个数据
- @ConfigurationProperties(prefix = "enterprise")
- public class Enterprise {
- private String name;
- private Integer age;
- private String tel;
- private String[] subject;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。