context:property-placeholder使用说明

作用

  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"/>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。