web程序中经常要加载properties文件,有时想让它映射成一个java pojo,但有时就是想要一个Properties对象。如下列举出两种方法:

方法一:

<util:properties id="jdbcProperties" location="classpath*:jdbc.properties" />

(注意,该方式需要在xml中配置xmlns xlns:util="http://www.springframework.org/schema/util")

然后在代码中使用

@Resource
private Properties jdbcProperties;

就能访问了。

方法二:

<bean id="jdbcProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath*:jdbc.properties</value>
        </list>
    </property>
</bean>

注入后就能使用了。