2.2.2.2运行结果:
![二 SpringBoot - 核心配置文件](http://shimg.jingyanzongjie.com/230725/1I6304U9-1.png)
文章插图
2.2.3${} 和 #{} 的区别
- ${}:用于读取核心配置文件中的自定义配置,也可以给属性指定默认值 (${xxx.xx:default值});
- #{}:不可以读取核心配置文件中的自定义配置,可以给属性发指定默认值#{default值} (可以使用表达式),还可以读取容器中已用实体的属性值;
- 两种读取自定义配置的方式,是可以混用的,但是实际开发中,尽量使用其中一种,,一般都是少量配置,单个读取,多个读取,使用批量读取;
student.studentId=19130010student.studentName=huayustudent.studentClass=计算机科学与技术(2)student.graduationSchool=金陵科技学院student.graduationTime=2023/7/1 12:12:12student.nativePlace=南京student.hasGirFriends=true
3.1.2 StudentProperties.java@Data@Component@ConfigurationProperties(prefix = "student")public class StudentProperties {// 学号private String studentId;// 姓名private String studentName;// 班级private String studentClass;// 毕业院校private String graduationSchool;// 毕业时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")private Date graduationTime;// 籍贯private String nativePlace;// 有没有女朋友private boolean hasGirFriends;}
3.1.3 StudentValues.java@Data@Component //第一个写法,使用普通组件@PropertySource(value = "https://www.huyubaike.com/biancheng/classpath:student.properties")//单个从student.properties 中获取参数public class StudentValues {// 学号@Value("${student.studentId}")private String studentId;// 姓名@Value("${student.studentName}")private String studentName;// 班级@Value("${student.studentClass}")private String studentClass;// 毕业院校@Value("${student.graduationSchool}")private String graduationSchool;// 毕业时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")@Value("${student.graduationTime}")private Date graduationTime;// 籍贯@Value("${student.nativePlace}")private String nativePlace;// 有没有女朋友@Value("${student.hasGirFriends}")private boolean hasGirFriends;}
3.2 xxx.yml3.2.1 student.ymlstudent:studentId: 19130010studentName: huayustudentClass: 计算机科学与技术(2)graduationSchool: 金陵科技学院graduationTime: 2023/7/1 12:12:12nativePlace: 南京hasGirFriends: true
3.2.2 StudentProperties.java@Data@Component@ConfigurationProperties(prefix = "student")@PropertySource(value = "https://www.huyubaike.com/biancheng/classpath:student.yml",encoding = "utf-8",factory = YamlPropertySourceFactory.class) //从自定义的 student.yml 中获取public class StudentProperties {......}
3.2.3 StudentValues.java@Data@Component@PropertySource(value = "https://www.huyubaike.com/biancheng/classpath:my.yml", factory = YamlPropertySourceFactory.class) //从自定义的 student.yml 中获取public class StudentValues {......}
3.2.4 YamlPropertySourceFactory.java yml配置映射类@PropertySource读取不能直接自定义yaml配置文件,需要自定义一个继承 PropertySourceFactory 的 YamlPropertySourceFactory 编写配置映射类public class YamlPropertySourceFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) {Resource resource = encodedResource.getResource();YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();factory.setResources(resource);Properties props = factory.getObject();return new PropertiesPropertySource(resource.getFilename(), props);}}
3.3 测试3.3.1 testStudentProperties![二 SpringBoot - 核心配置文件](http://shimg.jingyanzongjie.com/230725/1I63043Y-2.png)
文章插图
3.3.2 testStudentValues
![二 SpringBoot - 核心配置文件](http://shimg.jingyanzongjie.com/230725/1I6302X7-3.png)
经验总结扩展阅读
- .NET 采用 SkiaSharp 生成二维码和图形验证码及图片进行指定区域截取方法实现
- 2024年十月十二出生武姓女孩名字怎么取生辰八字五行查询
- 2024年十月十二出生何姓男孩名字叫什么好名字推荐
- 切好的土豆丝放冰箱第二天能用吗
- 卤水卤第二次还要放调料吗
- 2023年处暑后天气还热吗 二十四节气
- 高铁二等座能要毯子么
- 【算法训练营day1】LeetCode704. 二分查找 LeetCode27. 移除元素
- Nebula Graph介绍和SpringBoot环境连接和查询
- AVX图像算法优化系列二: 使用AVX2指令集加速查表算法。