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
}
}
网友评论