要了解這個類首先要弄清楚一個概念:bean factory post-processor
官方解釋是這樣的:
A bean factory post-processor is a java class which implements the
org.springframework.beans.factory.config.BeanFactoryPostProcessor interface. It is executed manually (in the case of the BeanFactory) or automatically (in the case of the ApplicationContext) to apply changes of some sort to an entire BeanFactory, after it has been constructed.
我理解的意思是這樣的:
1.首先bean factory post-processor實現了org.springframework.beans.factory.config.BeanFactoryPostProcessor接口。
2.在BeanFactory的情況下它被手動的執行。
3.在ApplicationContext的條件下它會自動的執行。
4.最關鍵的一點是,是在一個類的實例被構造出來之後,對整個BeanFactory進行修改。
那麼PropertyPlaceholderConfigurer類就是bean factory post-processor的一種,它的作用是一個資源屬性的配置器,能夠將BeanFactory的裏定義的內容放在一個以.propertis後綴的文件中。
例如
<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是怎樣引用的呢:
---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>
將上邊一段配置註冊在web.xml中就可以了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
當然,不要忘了spring的監聽器註冊
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
這樣,一個簡單的數據源就設置完畢了。
實際上,PropertyPlaceholderConfigurer起的作用就是將佔位符指向的數據庫配置信息放在bean中定義
的工具。
淺析Spring框架下PropertyPlaceholderConfigurer類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
JSP實現驗證碼
fengjingchao8
2018-08-27 19:48:48
利用JQuery框架ajax驗證
fengjingchao8
2018-08-27 19:48:45
struts-config.xml
fengjingchao8
2018-08-27 19:48:45
用O’Reilly公司的cos實現文件上傳
fengjingchao8
2018-08-27 19:48:44
Java 中 getResourceAsStream 的用法
fengjingchao8
2018-08-27 19:48:44
MyEclipse 中使用 CVS 進行版本控制
fengjingchao8
2018-08-27 19:48:44
struts action
fengjingchao8
2018-08-27 19:48:44
Java中的抽象函數與C++中的虛函數
fengjingchao8
2018-08-27 19:48:44
Struts1文件上傳
fengjingchao8
2018-08-27 19:48:44
Ajax裏的onreadystatechange
fengjingchao8
2018-08-27 19:48:44
Spring Web Flow 入門demo(一)簡單頁面跳轉
qq_34325222
2018-08-27 11:56:04
spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
qq_34325222
2018-08-27 11:55:57
G4J的NDC與MDC
qq_34325222
2018-08-27 11:55:55
web.xml文件中的jsp-config標籤詳解 --可用於解決jsp、html亂碼問題
qq_34325222
2018-08-27 11:55:55
Spring MVC
米糕思密达
2018-08-27 11:51:51