一、code
package com.test; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.builder.fluent.Configurations; import org.apache.commons.configuration2.ex.ConfigurationException; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Properties; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; public class Test { public static void main(String args[]){ Test test = new Test(); test.method1(); test.method2(); test.method3(); test.method4(); test.method5(); test.method6(); } public void method1(){ Properties properties = new Properties(); /* * InputStream inputStream = Test.class.getResourceAsStream("/db.properties"); * InputStream inputStream1 = Test.class.getClassLoader().getResourceAsStream("db.properties"); * 上面两种方式可以达到一样的效果*/ InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties"); try{ /*new Exception().getStackTrace()[0].getMethodName() 输出本方法名,返回值String new Exception().getStackTrace()[0].getMethodName() 输出调用者的方法名,返回值String * */ properties.load(inputStream); System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; " + "root:" + " " + properties.getProperty("root")); } catch(IOException e){ e.getMessage(); } } public void method2(){ Properties properties = new Properties(); try { //通过Spring的PropertiesLoaderUtils.loadAllProperties载入配置文件 properties = PropertiesLoaderUtils.loadAllProperties("db.properties"); System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "password:" + " " + properties.getProperty("password")); } catch (IOException e){ e.getMessage(); } } public void method3(){ Properties properties = new Properties(); try { //通过IO输入流载入配置 InputStream inStream = new FileInputStream(new File("src\\main\\resources\\db.properties")); properties.load(inStream); System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " + properties.getProperty("root")); } catch (IOException e){ e.getMessage(); } } public void method4(){ Properties properties = new Properties(); try { /* UrlResource urlResource = new UrlResource("file:target/classes/db.properties"); InputStream inStream = urlResource.getInputStream(); properties.load(inStream); System.out.println(properties.getProperty("password")); */ //通过URL读取配置文件,和上面的UrlResource达到的效果一致 URL url = new URL("file:src\\main\\resources\\db.properties"); InputStream inStream1 = url.openStream(); properties.load(inStream1); System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " + properties.getProperty("root")); } catch (MalformedURLException e){ e.getMessage(); } catch (IOException e){ e.getMessage(); } } public void method5(){ //通过java.util.ResourceBundle类加载配置文件,ResourceBundle.getBundle("db")中没有使用配置文件后缀名 ResourceBundle resource = ResourceBundle.getBundle("db"); System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " +resource.getString("root") ); //使用输入流方式载入配置文件 InputStream inputStream1 = Test.class.getClassLoader().getResourceAsStream("db.properties"); try { ResourceBundle resource1 = new PropertyResourceBundle(inputStream1); System.out.println("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "password:" + " " +resource1.getString("password")+"2"); } catch (IOException e){ e.getMessage(); } } public void method6(){ Configurations configs = new Configurations(); /* * 需要用到的commons-configuration2版本为2.0及以上,还需要用到commons-beanutils依赖包 */ try { Configuration config = configs.properties(new File("db.properties")); System.out.print("methodName:" + new Exception().getStackTrace()[0].getMethodName() + " ; "+ "root:" + " " +config.getString("root") ); } catch (ConfigurationException e){ e.getMessage(); } } }
二、输出截图
三、第六种方法的依赖包
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-configuration2</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.3</version> </dependency>