国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
PropertyPlaceholderConfigurer的用法講解
Spring的框架中為您提供了一個 BeanFactoryPostProcessor 的實作類別: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。藉由這個類別,您可以將一些組態(tài)設(shè)定,移出至.properties檔案中,如此的安排可以讓XML定義檔負(fù)責(zé)系統(tǒng)相關(guān)設(shè)定,而.properties檔可以作為客戶根據(jù)需求,自定義一些相關(guān)的參數(shù)。
來看一個Bean定義檔的實際例子:
  • beans-config.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>  
      <bean id="configBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

          <property name="location">
              <value>hello.properties</value>
          </property>
      </bean>

      <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
          <property name="helloWord">
              <value>${onlyfun.caterpillar.helloWord}</value>
          </property>
      </bean>
</beans>

假設(shè)在helloBean中有許多依賴注入的屬性,這些都是比較不常變動的屬性,而其中helloWord會經(jīng)常變動,可以透過hello.properties來簡單的設(shè)定,而這個資訊已設(shè)定在configBean的location屬性中:
  • hello.properties
onlyfun.caterpillar.helloWord=Welcome!

在helloBean的helloWord屬性中,${onlyfun.caterpillar.helloWord}將會被hello.properties的helloWord所取代。

如果有多個.properties檔案,則可以透過locations屬性來設(shè)定,例如:

  • beans-config.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>  
      <bean id="configBean"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
              <list>
                  <value>hello.properties</value>
                  <value>welcome.properties</value>
                  <value>other.properties</value>
    
              </list>
          </property>     
      </bean>

      <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">
          <property name="helloWord">
              <value>${onlyfun.caterpillar.helloWord}</value>
          </property>
          ...
      </bean>
</beans>
 
======================================
PropertyPlaceholderConfigurer類就是bean factory post-processor的一種,它的作用是一個資源屬性的配置器,能夠?qū)eanFactory的里定義的內(nèi)容放在一個以.propertis后綴的文件中。
例如
---spring-context.xml----
<bean id="propertyConfigurer"   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
             <list>
                 <value>/WEB-INF/jdbc.properties</value>
             </list>
         </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"><value>${driver}</value></property>
    <property name="url"><value>jdbc:${dbname}</value></property>
</bean>

而實際的jdbc.propertis文件是
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
而jdbc.propertis是怎樣引用的呢:
 

將上邊一段配置注冊在web.xml中就可以了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
當(dāng)然,不要忘了spring的監(jiān)聽器注冊
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
這樣,一個簡單的數(shù)據(jù)源就設(shè)置完畢了。
實際上,PropertyPlaceholderConfigurer起的作用就是將占位符指向的數(shù)據(jù)庫配置信息放在bean中定義
的工具。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Spring入門16 - BeanFactoryPostProcessor接口
淺析Spring框架下PropertyPlaceholderConfigurer類
一個spring的簡單例子
Spring 中 用 ${xxx} 讀取properties文件的說明 配置
Spring的核心容器Bean
spring 配置文件管理
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服