zoukankan      html  css  js  c++  java
  • spring中constructor-arg 构造方法注入

    spring中constructor-arg 构造方法注入。在容器中生成bean时,用到constructor-arg构造注入时,该类必须要有相对应的构造方法,如果constructor-arg有参数,那么类中必须写有相对应参数的构造方法

         // 使用构造子注入时,则使用constructor-arg子标签,来指定构造函数的参数。   
    <bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
        <constructor-arg>   
            <value>This is a configurable message</value>   
        </constructor-arg>   
    </bean>   
          //当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始。   
    <bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
        <constructor-arg index="0">   
            <value>first parameter</value>   
        </constructor-arg>   
        <constructor-arg index="1">   
            <value>second parameter</value>   
        </constructor-arg>   
      
    </bean>   
        // 在使用构造子注入时,需要注意的问题是要避免构造子冲突的情况发生。考虑下面的情况:   
    public class ConstructorConfusion {   
        public ConstructorConfusion(String someValue) {   
            System.out.println("ConstructorConfusion(String) called");   
        }   
        public ConstructorConfusion(int someValue) {   
            System.out.println("ConstructorConfusion(int) called");   
        }   
    }   
        // 使用如下配置文件   
    <bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
        <constructor-arg>   
            <value>90</value>   
        </constructor-arg>   
    </bean>   
        // 那么,当实例化组件constructorConfusion时,将输出ConstructorConfusion(String) called,也就是说参数类型为String的构造函数被调用了,这显然不符合我们的要求。为了让Spring调用参数为int的构造函数来实例化组件constructorConfusion,我们需要在配置文件中明确的告诉Spring,需要使用哪个构造函数,这需要使用constructor-arg的type属性。   
    <bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
        <constructor-arg type="int">   
            <value>90</value>   
        </constructor-arg>   
    </bean>   
         //我们不仅可以构造单个BeanFactory,而且可以建立有继承关系的多个BeanFactory。只需要将父BeanFactory作为参数传给子BeanFactory的构造函数即可。   
    BeanFactory parent =   
        new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/parent.xml"));   
    BeanFactory child =   
        new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/beans.xml"), parent);   
         //如果子BeanFactory和父BeanFactory中含有名称相同的Bean,那么在子BeanFactory中使用   
    <ref bean="sameNameBean"/&gt;//引用的将是子BeanFactory中的bean,为了引用父BeanFactory中的bean,我们需要使用ref标签的parent属性,<ref parent="sameNameBean"/>。   
         为了注入集合属性,Spring提供了list,map,set和props标签,分别对应List,Map,Set和Properties,我们甚至可以嵌套的使用它们(List of Maps of Sets of Lists)。   
    <bean id="injectCollection" class="com.apress.prospring.ch4.CollectionInjection">   
        <property name="map">   
            <map>   
                <entry key="someValue">   
                    <value>Hello World!</value>   
                </entry>   
                <entry key="someBean">   
                    <ref local="oracle"/>   
                 </entry>   
            </map>   
        </property>   
        <property name="props">   
            <props>   
                <prop key="firstName">   
                    Rob   
                </prop>   
                <prop key="secondName">   
                    Harrop   
                </prop>   
            </props>   
        </property>   
        <property name="set">   
            <set>   
                <value>Hello World!</value>   
                <ref local="oracle"/>   
            </set>   
        </property>   
        <property name="list">   
            <list>   
                <value>Hello World!</value>   
                <ref local="oracle"/>   
             </list>   
        </property>   
    </bean>   
    /************************* 
  • 相关阅读:
    Oracle 删除重复数据的几种方法
    12.25模拟赛T3
    java实现第五届蓝桥杯圆周率
    java实现第五届蓝桥杯武功秘籍
    Oracle 审计初步使用
    [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
    ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database
    12.25模拟赛T2
    java实现第五届蓝桥杯写日志
    java实现第五届蓝桥杯李白打酒
  • 原文地址:https://www.cnblogs.com/sanhuan/p/4789054.html
Copyright © 2011-2022 走看看