`

PropertiesLoaderUtils

 
阅读更多
/**
 * Convenient utility methods for loading of <code>java.util.Properties</code>,
 * performing standard handling of input streams.
 *
 * <p>For more configurable properties loading, including the option of a
 * customized encoding, consider using the PropertiesLoaderSupport class.
 *
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @since 2.0
 * @see PropertiesLoaderSupport
 */
public abstract class PropertiesLoaderUtils {

	/**
	 * Load properties from the given resource.
	 * @param resource the resource to load from
	 * @return the populated Properties instance
	 * @throws IOException if loading failed
	 */
	public static Properties loadProperties(Resource resource) throws IOException {
		Properties props = new Properties();
		fillProperties(props, resource);
		return props;
	}

	/**
	 * Fill the given properties from the given resource.
	 * @param props the Properties instance to fill
	 * @param resource the resource to load from
	 * @throws IOException if loading failed
	 */
	public static void fillProperties(Properties props, Resource resource) throws IOException {
		InputStream is = resource.getInputStream();
		try {
			props.load(is);
		}
		finally {
			is.close();
		}
	}

	/**
	 * Load all properties from the given class path resource,
	 * using the default class loader.
	 * <p>Merges properties if more than one resource of the same name
	 * found in the class path.
	 * @param resourceName the name of the class path resource
	 * @return the populated Properties instance
	 * @throws IOException if loading failed
	 */
	public static Properties loadAllProperties(String resourceName) throws IOException {
		return loadAllProperties(resourceName, null);
	}

	/**
	 * Load all properties from the given class path resource,
	 * using the given class loader.
	 * <p>Merges properties if more than one resource of the same name
	 * found in the class path.
	 * @param resourceName the name of the class path resource
	 * @param classLoader the ClassLoader to use for loading
	 * (or <code>null</code> to use the default class loader)
	 * @return the populated Properties instance
	 * @throws IOException if loading failed
	 */
	public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException {
		Assert.notNull(resourceName, "Resource name must not be null");
		ClassLoader clToUse = classLoader;
		if (clToUse == null) {
			clToUse = ClassUtils.getDefaultClassLoader();
		}
		Properties properties = new Properties();
		Enumeration urls = clToUse.getResources(resourceName);
		while (urls.hasMoreElements()) {
			URL url = (URL) urls.nextElement();
			InputStream is = null;
			try {
				URLConnection con = url.openConnection();
				con.setUseCaches(false);
				is = con.getInputStream();
				properties.load(is);
			}
			finally {
				if (is != null) {
					is.close();
				}
			}
		}
		return properties;
	}

}

 

分享到:
评论

相关推荐

    PropertiesLoaderUtils 出现中文乱码的解决方式

    PropertiesLoaderUtils 中文乱码解决方式详解 PropertiesLoaderUtils 是 Spring 框架中的一种 properties 加载工具类,用于加载 properties 文件中的配置信息。然而,在使用 PropertiesLoaderUtils 加载 properties...

    JAVA 解决Properties文件保存中文乱码

    props = PropertiesLoaderUtils.loadProperties(new Resource[] { new ClassPathResource("config.properties", StandardCharsets.UTF_8) }); String value = props.getProperty("key"); System.out.println...

    详解spring boot 使用application.properties 进行外部配置

    props = PropertiesLoaderUtils.loadProperties(resource); } catch (IOException e) { e.printStackTrace(); } } public static String getProperty(String key) { return props.getProperty(key); } } `...

    GetYamlUtil.rar

    import org.springframework.core.io.support.PropertiesLoaderUtils; import org.yaml.snakeyaml.Yaml; import java.io.IOException; import java.util.Map; public class GetYamlUtil { public static Map, ...

    java读取配置文件

    Properties props = PropertiesLoaderUtils.loadProperties(resource); String dbUrl = props.getProperty("db.url"); ``` 5. **使用`java.nio`包** Java NIO(New IO)提供了非阻塞的I/O操作,可以更高效地...

    SPRING API 2.0.CHM

    PropertiesLoaderUtils PropertiesMethodNameResolver PropertiesPersister PropertyAccessException PropertyAccessor PropertyAccessorUtils PropertyBatchUpdateException PropertyComparator ...

    使用spring工厂读取property配置文件示例代码

    这里我们创建了一个名为`Test`的类,其中包含一个`main`方法,演示了如何使用`PropertiesLoaderUtils`来加载和读取配置文件。`PropertiesLoaderUtils`是Spring提供的工具类,可以方便地从各种资源加载`Properties`...

    Spring项目里将SQL语句写在.sql文件中的方法

    在这个实现中,我们首先使用`PropertiesLoaderUtils.fillProperties`加载.sql文件的内容到`Properties`对象。然后,遍历`Properties`,筛选出以`--!`开头的键,并将其值放入一个`Map`中。最后,创建一个`...

Global site tag (gtag.js) - Google Analytics