使用spring读取properties文件,springproperties,spring可以在配置文
分享于 点击 28789 次 点评:211
使用spring读取properties文件,springproperties,spring可以在配置文
spring可以在配置文件中使用properties文件中配置的项,但是有时候我们也需要直接通过代码去存取配置文件中的项,该怎么做呢?
其实也很简单,spring提供了PropertiesLoaderUtils
类,此类可以方便的载入properties配置文件,如下实例代码:
Resource resource = new ClassPathResource("/my.properties");Properties props = PropertiesLoaderUtils.loadProperties(resource);
需要引用:
import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PropertiesLoaderUtils;
用户点评