spring配置:context:property-placeholder 讀取配置文件信息 在配置文件中使用el表達式填充值


spring將properties文件讀取后在配置文件中直接將對象的配置信息填充到bean中的變量里。

原本使用PropertyPlaceholderConfigurer類進行文件信息配置。PropertyPlaceholderConfigurer實現了BeanFactoryPostProcessor接口,能夠對<bean/>中的屬性值進行外在化管理。
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <value>userinfo.properties</value>
    </list>
  </property>
</bean>
<bean name="userInfo" class="test.UserInfo">
  <property name="username" value="${db.username}"/>
  <property name="password" value="${db.password}"/>
</bean>

現在,直接使用
<context:property-placeholder location="classpath*:resources/*.properties" />

注意:spring容器中最多只能定義一個context:property-placeholder。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM