Class loader to load configuration file

// Create a db.properties resource file in the folder

Which kept the two values:

// Load the contents of the file:

@RunWith (SpringRunner.class) 
@SpringBootTest 
public class JestTest { 


    @Test 
    public void getProperties () throws IOException { 
        // get the class loader 2 ways, then the configuration file can be loaded in, a Thread class is acquired, other a bytecode object acquisition 
        ClassLoader Loader = Thread.currentThread () getContextClassLoader ();. 
        // = JestTest.class.getClassLoader ClassLoader Loader (); 
        the inputStream inputStream = loader.getResourceAsStream ( "the db.properties"); // read take input stream files into 
        the Properties new new P = the Properties (); 
        p.load (inputStream); 
        System.out.println (p.get ( "name")); Test // 
        System.out.println (p.get ( "url")); // 127.0.0.1 
        System.out.println (p.get ( "password"));//null
//null
    }
}

  

Guess you like

Origin www.cnblogs.com/yangxiaohui227/p/11200175.html