使用spring读取properties文件

spring可以在配置文件中使用properties文件中配置的项,但是有时候我们也需要直接通过代码去存取配置文件中的项,该怎么做呢?

其实也很简单,spring提供了PropertiesLoaderUtils类,此类可以方便的载入properties配置文件,如下实例代码:

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


Resource resource = new ClassPathResource("/my.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);

 

猜你喜欢

转载自zus.iteye.com/blog/2266189