PropertyPlaceholderConfigurer在spring获取属性文件

首先你先在.properties文件中以键值对的形式写好之后(我们方便在.properties文件中写数据库配置),

在spring配置文件先将这个资源文件初始化成bean,然后再在XML中以${……}的形式取出里边的值(习惯在applicationContext.xml中配置数据源)。

在spring中的配置是:
<!-- 配置的资源文件 -->
       <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfi gurer">
              <property name="locations">
                     <list>
                            <value>classpath:/system.properties</value>
                     </list>
              </property>
       </bean>

在properties文件中书写格式:
informationSerHessian.serverName = 127.0.0.1

在其他的xml中使用方式

${informationSerHessian.serverName}取出里边的值


实例:

属性文件设置:



配置spring自带的属性文件读取类PropertyPlaceholderConfigurer


子类重写了PropertyPlaceholderConfigurer中方法


在xml中通过属性文件的key(DB.username等)获取数值value(jtyj)




猜你喜欢

转载自blog.csdn.net/hanlt/article/details/51980661