二 SpringBoot - 核心配置文件( 二 )

2.2.2.2运行结果:

二 SpringBoot - 核心配置文件

文章插图
2.2.3${} 和 #{} 的区别
  1. ${}:用于读取核心配置文件中的自定义配置,也可以给属性指定默认值 (${xxx.xx:default值});
  2. #{}:不可以读取核心配置文件中的自定义配置,可以给属性发指定默认值#{default值} (可以使用表达式),还可以读取容器中已用实体的属性值;
  3. 两种读取自定义配置的方式,是可以混用的,但是实际开发中,尽量使用其中一种,,一般都是少量配置,单个读取,多个读取,使用批量读取;
3、自定义配置文件并获取配置信息3.1xxx.properties3.1.1 student.propertiesstudent.studentId=19130010student.studentName=huayustudent.studentClass=计算机科学与技术(2)student.graduationSchool=金陵科技学院student.graduationTime=2023/7/1 12:12:12student.nativePlace=南京student.hasGirFriends=true3.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: true3.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 - 核心配置文件

文章插图
3.3.2 testStudentValues
二 SpringBoot - 核心配置文件

经验总结扩展阅读