XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)

承接上文

IObjectFactoryPostProcessor(工厂后处理器))


前提是实现接口的对象注册给当前容器

  1. 直接继承的对象调用这个postProcessBeanFactory方法,参数为工厂
  2. 添加对象配置在xml中用IApplicationContext自动注册
  1. 接口名称分别为.net的Spring.Objects.Factory.Config.IObjectFactoryPostProcessor 与Java的org.springframework.beans.factory.config.BeanFactoryPostProcessor

    postProcessBeanFactory就一个方法,参数为创建工厂的对象, 方法的作用就是可以在对象定义被载入容器之后并且在被实例化之前执行某些行为

  2. spring提供的实现类(两个接口的实现类唯一的不同点是一个 是用占位符一个是直接用实例后对象的属性名)

    1. PropertyPlaceholderConfigurer,其中基本用法如下,默认当变量不存在对应的值的时候会获取系统的环境变量

      C#:这个类的参数是键值对存在的,configSections属性设置了那个节点为要获取的键值对, EnvironmentVariableMode为枚举,有三个可用值:Never、Fallback和Override。 其中Fallback是默认值,会尝试用环境变量替换未在自定义键值对中找到匹配项的占位符 ;Override则不理会自定义键值对中是否有与占位符匹配的键,直接用环境变量值替换占位符; Nerver即不使用环境变量进行替换

      <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设置是否搜索系统环境变量为布尔类型

      <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.properties

      jdbc=aaa
      jdbc.url=aaa
    2. PropertyOverrideConfigurer(和前面的区别替换的依据不是占位符,而是属性名)

      C#:其中的key为对象的属性名

      <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:

      <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.properties

      jdbc.url=aaa

原文地址:https://www.cnblogs.com/cnlj/p/3477193.html