context:property-placeholder使用说明

2018-11-20  本文已影响0人  coderLumia

作用

  1. 有些参数在某些阶段中是常量。
    如在开发阶段我们连接数据库时的url,username,password等信息,
    分布式应用中client端的server地址,端口等
    2.这些参数在不同阶段之间又往往需要改变
    我们可以将这些信息写入到配置文件中,通过spring加载到容器进行使用,在spring3中提供了一种简便的方式就是使用<context:property-placeholder>元素

配置

<context:property-placeholder location="classpath*:properties/redis.properties" 
ignore-unresolvable="true" order="2"></context:property-placeholder>

通过以上配置加载redis的相关配置信息

<bean id="propertyPlaceholderConfigurer" 
class="org.springframework,beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:properties/redis.properties<value/>
        </list>
    </property>
</bean>

注意:一个spring容器中只能有一个propertyPlaceholdedConfigurer的bean或者一个<context:property-placeholder>,若有多个文件需要加载,或者在多模块开发中可以使用

<context:property-placeholder location="classpath*:*.properties"/>
上一篇下一篇

猜你喜欢

热点阅读