Start by creating a new Test.yaml file in the project SRC directory.
The code is as follows:
Spring: application: name:cruncher DataSource: driverClassName:com.mysql.jdbc.Driver URL: Jdbc:mysql://localhost/testserver: port:9000
Then the test code:
PackageCom.ming.yaml;ImportJava.io.FileInputStream;ImportJava.net.URL;ImportJava.util.Map;ImportOrg.yaml.snakeyaml.Yaml; Public classTest { Public Static voidMain (string[] args) {Try{Yaml Yaml=NewYaml (); URL URL= Test.class. getClassLoader (). GetResource ("Test.yaml"); if(URL! =NULL) { //gets the configuration data from the Test.yaml file, and then converts it to obj,Object obj =yaml.load (NewFileInputStream (Url.getfile ())); System.out.println (obj); //You can also convert a value to a mapMap map = (map) yaml.load (NewFileInputStream (Url.getfile ())); SYSTEM.OUT.PRINTLN (map); //we can take the value by map. } } Catch(Exception e) {e.printstacktrace (); } }}
The results of the operation are as follows:
Summary: By getting the configuration properties of the Yaml file, you can take a value from map, and if you want to convert the obtained string to an object, you may go through the third-party plug-in library. This yaml file is really much more powerful than a property file.
Java implementation reads the Yaml file and gets the value