zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【内存泄漏】方法三:利用linux的valgrind命令定位内存泄露(Memory Leak)
    【内存泄漏】方法二:利用linux的mtrace命令定位内存泄露(Memory Leak)
    Windows下sqlmap的使用_01
    关于安装和使用BurpSuite及Java环境的配置问题
    Windows下修改环境变量的几种方法
    OPPO VOOC快充原理
    在线代码运行
    Linux在线学习模拟器
    socket设置为非阻塞模式
    pthread_mutexattr_t设置的相关函数及其说明
  • 原文地址:https://www.cnblogs.com/cnlj/p/3477193.html
Copyright © 2011-2022 走看看