网站规划与建设实验心得,美图在线图片编辑器,玩具网站建设策划书流程,个人做视频网站怀旧网个人博客地址#xff1a;怀旧网#xff0c;博客详情#xff1a;SpringBoot 在初始化加载无法使用Value的时候读取配置文件教程
读取数据库数据案例
// 创建YamlPropertiesFactoryBean对象
YamlPropertiesFactoryBean factory new YamlPropertiesFactoryBean();
// …怀旧网个人博客地址怀旧网博客详情SpringBoot 在初始化加载无法使用Value的时候读取配置文件教程
读取数据库数据案例
// 创建YamlPropertiesFactoryBean对象
YamlPropertiesFactoryBean factory new YamlPropertiesFactoryBean();
// 设置要读取的YAML文件路径
factory.setResources(new ClassPathResource(application.yml));// 读取YAML文件内容并转换为Properties对象
Properties properties factory.getObject();// 读取配置属性
String url properties.getProperty(spring.datasource.url);
String username properties.getProperty(spring.datasource.username);
String password properties.getProperty(spring.datasource.password);connection DriverManager.getConnection(url, username, password);在上述代码中我们使用YamlPropertiesFactoryBean类来读取YAML文件并将其转换为Properties对象。首先创建YamlPropertiesFactoryBean对象然后使用setResources()方法设置要读取的YAML文件路径这里使用ClassPathResource来读取config.yml文件。接下来通过getObject()方法获取Properties对象然后可以使用getProperty()方法读取配置属性的值。 请将config.yml替换为你实际的YAML文件名并确保文件位于resources目录下的正确路径中。