欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Spring使用程序方式读取properties文件,springproperties,在spring中可以通过

来源: javaer 分享于  点击 5406 次 点评:174

Spring使用程序方式读取properties文件,springproperties,在spring中可以通过


在spring中可以通过下面的方式将配置文件中的项注入到配置中

       <bean        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />        <property name="ignoreResourceNotFound" value="true" />        <property name="locations">            <list>                <!-- standard config -->                <value>classpath*:application.properties</value>            </list>        </property>    </bean>    <bean id="cacheManager" class="cn.outofmemory.util.MemCacheManager" init-method="init">        <property name="nodeList"  value="${memcache.nodelist}"/>        <property name="initConn" value="${memcache.initConn}"/>        <property name="minConn" value="${memcache.minConn}"/>        <property name="maxConn" value="${memcache.maxConn}"/>        <property name="maxIdle" value="${memcache.maxIdle}"/>        <property name="maintSleep" value="${memcache.maintSleep}"/>    </bean>

但是这样的注入没有办法通过程序来访问properties文件中的内容,spring还提供了 org.springframework.core.io.support.PropertiesLoaderUtils类可以方便的载入配置文件,如下两行代码:

Resource resource = new ClassPathResource("/application.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;
相关栏目:

用户点评