前提是實現接口的對象註冊給當前容器 html
- 直接繼承的對象調用這個postProcessBeanFactory方法,參數爲工廠
- 添加對象配置在xml中用IApplicationContext自動註冊
接口名稱分別爲.net的Spring.Objects.Factory.Config.IObjectFactoryPostProcessor 與Java的org.springframework.beans.factory.config.BeanFactoryPostProcessorjava
postProcessBeanFactory就一個方法,參數爲建立工廠的對象, 方法的做用就是能夠在對象定義被載入容器以後而且在被實例化以前執行某些行爲spring
spring提供的實現類(兩個接口的實現類惟一的不一樣點是一個 是用佔位符一個是直接用實例後對象的屬性名)ide
- PropertyPlaceholderConfigurer,其中基本用法以下,默認當變量不存在對應的值的時候會獲取系統的環境變量
C#:這個類的參數是鍵值對存在的,configSections屬性設置了那個節點爲要獲取的鍵值對, EnvironmentVariableMode爲枚舉,有三個可用值:Never、Fallback和Override。 其中Fallback是默認值,會嘗試用環境變量替換未在自定義鍵值對中找到匹配項的佔位符 ;Override則不理會自定義鍵值對中是否有與佔位符匹配的鍵,直接用環境變量值替換佔位符; Nerver即不使用環境變量進行替換post
<configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> </sectionGroup> <section name="DaoConfiguration" type="System.Configuration.NameValueSectionHandler"/> <section name="DatabaseConfiguration" type="System.Configuration.NameValueSectionHandler"/> </configSections> <DaoConfiguration> <add key="maxResults" value="1000"/> </DaoConfiguration> <DatabaseConfiguration> <add key="connection.string" value="dsn=MyDSN;uid=sa;pwd=myPassword;"/> </DatabaseConfiguration> <spring> <context> <resource uri="config://spring/objects"/> </context> <objects> <object type="SpringBase.test,SpringBase"> <property name="result" value="${maxResults}"/> <property name="connectionstring" value="${connection.string}"/> </object> <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core"> <property name="configSections"> <value>DaoConfiguration,DatabaseConfiguration</value> </property> </object> </objects> </spring> </configuration>java:其中classpath前綴表示在類文件地址開始, searchSystemEnvironment設置是否搜索系統環境變量爲布爾類型ui
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean> <bean class="springdemo.test" singleton="false"> <property name="name" value="${jdbc}" /> <property name="url" value="${jdbc.url}" /> </bean>config.propertiesurl
jdbc=aaa jdbc.url=aaa- PropertyOverrideConfigurer(和前面的區別替換的依據不是佔位符,而是屬性名)
C#:其中的key爲對象的屬性名spa
<configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> </sectionGroup> <section name="DaoConfigurationOverride" type="System.Configuration.NameValueSectionHandler"/> </configSections> <DaoConfigurationOverride> <add key="test.result" value="1000"/> </DaoConfigurationOverride> <spring> <context> <resource uri="config://spring/objects"/> </context> <objects> <object id="test" type="SpringBase.test,SpringBase"> <property name="result" value="11"/> </object> <object type="Spring.Objects.Factory.Config.PropertyOverrideConfigurer, Spring.Core"> <property name="configSections"> <value>DaoConfigurationOverride</value> </property> </object> </objects> </spring> </configuration>java:.net
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean> <bean id="jdbc" class="springdemo.test" singleton="false"> <property name="url" value="123" /> </bean>config.propertiescode
jdbc.url=aaa