Spring 属性文件加载工具类 PropertiesLoaderUtils
Spring 属性文件加载工具类 PropertiesLoaderUtils
1、简介
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/support/PropertiesLoaderUtils.html
这个工具类主要是针对Properties文件的加载操作,在Spring对.properties、 .componet文件和.factories文件的操作都有使用到。
先来简单看看这个类提供的有用方法:
- Properties loadProperties(Resource resource) throws IOException:从一个资源文件加载Properties;
- Properties loadProperties(EncodedResource resource) throws IOException:加载资源文件,传入的是提供了编码的资源类(EncodedResource);和上面方法基本一致;
- void fillProperties(Properties props, Resource resource) throws IOException:从一个资源类中加载资源,并填充到指定的Properties对象中;
- void fillProperties(Properties props, EncodedResource resource) throws IOException:从一个编码资源类中加载资源,并填充到指定的Properties对象中;和上面方法基本一致;
- Properties loadAllProperties(String resourceName) throws IOException:根据资源文件名称,加载并合并classpath中的所有资源文件;
- Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException:从指定的ClassLoader中,根据资源文件名称,加载并合并classpath中的所有资源文件;
2、在Spring 中的使用案例
public static final String COMPONENTS_RESOURCE_LOCATION = "META-INF/spring.components";
ClassLoader classLoader;
Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);
if (!urls.hasMoreElements()) {
return null;
}
List<Properties> result = new ArrayList<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
result.add(properties);
}
3、其他方式读取properties文件
public class PropertiesUtil {
/**
* 1. 方式一
*
* 从当前的类加载器的getResourcesAsStream来获取
* InputStream inputStream = this.getClass().getResourceAsStream(name)
*
* @throws IOException
*/
public void test1() throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("jdbc.url");
System.out.println("property = " + property);
}
/**
* 2. 方式二
*
* 从当前的类加载器的getResourcesAsStream来获取
* InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name)
*
* @throws IOException
*/
public void test5() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
/**
* 3. 方式三
*
* 使用Class类的getSystemResourceAsStream方法 和使用当前类的ClassLoader是一样的
* InputStream inputStream = ClassLoader.getSystemResourceAsStream(name)
*
* @throws IOException
*/
public void test4() throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
/**
* 4. 方式四
*
* Resource resource = new ClassPathResource(path)
* @throws IOException
*/
public void test2() throws IOException {
Resource resource = new ClassPathResource("config/application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
/**
* 5. 方式五
*
* 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别
* BufferedInputStream继承自InputStream
* InputStream inputStream = new BufferedInputStream(new FileInputStream(name)
* 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活
* @throws IOException
*/
public void test3() throws IOException {
InputStream inputStream = new BufferedInputStream(new FileInputStream("src/main/resources/config/application.properties"));
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
/**
* 6. 方式六
*
* 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别
* FileInputStream继承自InputStream
* InputStream inputStream = new FileInputStream(name)
* 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活
* @throws IOException
*/
public void test6() throws IOException {
InputStream inputStream = new FileInputStream("src/main/resources/config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
/**
* 7. 方式七
*
* 使用InputStream流来进行操作ResourceBundle,获取流的方式由以上几种。
* ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
* @throws IOException
*/
public void test7() throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties");
ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
Enumeration<String> keys = resourceBundle.getKeys();
while (keys.hasMoreElements()) {
String s = keys.nextElement();
System.out.println(s + " = " + resourceBundle.getString(s));
}
}
/**
* 8. 方式八
*
* ResourceBundle.getBundle的路径访问和 Class.getClassLoader.getResourceAsStream类似,默认从根目录下读取,也可以读取resources目录下的文件
* ResourceBundle rb = ResourceBundle.getBundle("b") //不需要指定文件名的后缀,只需要写文件名前缀即可
*/
public void test8(){
//ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //读取resources目录下的jdbc.properties
//读取resources/config目录下的application.properties
ResourceBundle rb2 = ResourceBundle.getBundle("config/application");
for(String key : rb2.keySet()){
String value = rb2.getString(key);
System.out.println(key + ":" + value);
}
}
}