spring中的PropertyPlaceholderConfigurer类

PropertyPlaceholderConfigurer类的主要的用法是将BeanFactory里定义的内容放在一个.properties的文件中.
比如,本来BeanFactory(设BeanFactory的配置文件名字为spring.xml)中的代码为
<bean id="" class="">
    <property name="user">
             <value>liming</value>
    </property>
    <property name="password">
             <value>123456</value>
    </property>
</bean>

使用PropertyPlaceholderConfigurer类后可以改成
<bean id="propertyConfigurer" class="org.springframework.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
          <list>
             <value>classpath:config.properties</value>
          <list>
    </property>
</bean>

而config.properties的内容为
user=liming
password=123456


当然,仅仅这样还不够,因为框架不会无缘无故创建你的propertyConfigurer类,因此还需要在web.xml中配置一下,代码如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring.xml</param-value>
</context-param>

<listener>
<listener-calss>org.springframework.web.context.ContextLoaderListener</listener-calss>
</listener>


通过以上的操作,我们可以将一些属性的配置放到了properties文件中(由于PropertyPlaceholderCOnfigurer是在其他bean实例化之前进行的,所以对于项目中所有bean的配置,他们的属性值都可以是properties文件中的内容,很强大有木有?)

猜你喜欢

转载自yizhenn.iteye.com/blog/2170500