美文网首页
原生的yml解析器

原生的yml解析器

作者: hemingkung | 来源:发表于2024-12-22 14:03 被阅读0次
import org.springframework.core.io.support.YamlPropertySourceLoader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.Map;

public class YamlToMap {
    public static void main(String[] args) throws IOException {
        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        ClassPathResource resource = new ClassPathResource("application-dev.yml");

        Assert.notNull(resource, "Could not find 'application-dev.yml'");

        Map<String, Object> data = loader.load("application-dev", resource, new HashMap<>()).get(0).getSource();

        // 获取spring下的datasource下的url
        String datasourceUrl = (String) data.get("spring.datasource.url");
        System.out.println(datasourceUrl); // 输出 jdbc:mysql://localhost:3306/yourdb
    }
}

相关文章

网友评论

      本文标题:原生的yml解析器

      本文链接:https://www.haomeiwen.com/subject/sbtuejtx.html