spring - constructor-arg 的使用

from http://hi.baidu.com/at87958208/item/dbd64575259291460d0a0724

 Spring使用spring-beans.dtd文件来定义BeanFactory的XML配置规范。可以在http://www.springframework.org/dtd/spring-beans.dtd找到该dtd文件,当然,Spring的下载文件中也已经包含了该dtd文件。它被放在dist文件夹中。 
      配置文件的根元素是beans,每个组件使用bean元素来定义,bean元素可以有许多属性,其中有两个是必须的:id和class(这里说的id是必须的并不意味着在配置文件中必须指定id,后面会详细说明)。id表示组件的默认名称,class表示组件的类型。 
      如果使用设值注入,则需要使用property子标签,来指定组件的属性。 

Java代码  
  1. <bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer">   
  2.     <property name="messageProvider">   
  3.         <ref local="provider"/>   
  4.     </property>   
  5. </bean>   
  6.      // 使用构造子注入时,则使用constructor-arg子标签,来指定构造函数的参数。   
  7. <bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
  8.     <constructor-arg>   
  9.         <value>This is a configurable message</value>   
  10.     </constructor-arg>   
  11. </bean>   
  12.       //当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始。   
  13. <bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
  14.     <constructor-arg index="0">   
  15.         <value>first parameter</value>   
  16.     </constructor-arg>   
  17.     <constructor-arg index="1">   
  18.         <value>second parameter</value>   
  19.     </constructor-arg>   
  20.   
  21. </bean>   
  22.     // 在使用构造子注入时,需要注意的问题是要避免构造子冲突的情况发生。考虑下面的情况:   
  23. public class ConstructorConfusion {   
  24.     public ConstructorConfusion(String someValue) {   
  25.         System.out.println("ConstructorConfusion(String) called");   
  26.     }   
  27.     public ConstructorConfusion(int someValue) {   
  28.         System.out.println("ConstructorConfusion(int) called");   
  29.     }   
  30. }   
  31.     // 使用如下配置文件   
  32. <bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
  33.     <constructor-arg>   
  34.         <value>90</value>   
  35.     </constructor-arg>   
  36. </bean>   
  37.     // 那么,当实例化组件constructorConfusion时,将输出ConstructorConfusion(String) called,也就是说参数类型为String的构造函数被调用了,这显然不符合我们的要求。为了让Spring调用参数为int的构造函数来实例化组件constructorConfusion,我们需要在配置文件中明确的告诉Spring,需要使用哪个构造函数,这需要使用constructor-arg的type属性。   
  38. <bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
  39.     <constructor-arg type="int">   
  40.         <value>90</value>   
  41.     </constructor-arg>   
  42. </bean>   
  43.      //我们不仅可以构造单个BeanFactory,而且可以建立有继承关系的多个BeanFactory。只需要将父BeanFactory作为参数传给子BeanFactory的构造函数即可。   
  44. BeanFactory parent =   
  45.     new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/parent.xml"));   
  46. BeanFactory child =   
  47.     new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/beans.xml"), parent);   
  48.      //如果子BeanFactory和父BeanFactory中含有名称相同的Bean,那么在子BeanFactory中使用   
  49. <ref bean="sameNameBean"/&gt;//引用的将是子BeanFactory中的bean,为了引用父BeanFactory中的bean,我们需要使用ref标签的parent属性,<ref parent="sameNameBean"/>。   
  50.      为了注入集合属性,Spring提供了list,map,set和props标签,分别对应List,Map,Set和Properties,我们甚至可以嵌套的使用它们(List of Maps of Sets of Lists)。   
  51. <bean id="injectCollection" class="com.apress.prospring.ch4.CollectionInjection">   
  52.     <property name="map">   
  53.         <map>   
  54.             <entry key="someValue">   
  55.                 <value>Hello World!</value>   
  56.             </entry>   
  57.             <entry key="someBean">   
  58.                 <ref local="oracle"/>   
  59.              </entry>   
  60.         </map>   
  61.     </property>   
  62.     <property name="props">   
  63.         <props>   
  64.             <prop key="firstName">   
  65.                 Rob   
  66.             </prop>   
  67.             <prop key="secondName">   
  68.                 Harrop   
  69.             </prop>   
  70.         </props>   
  71.     </property>   
  72.     <property name="set">   
  73.         <set>   
  74.             <value>Hello World!</value>   
  75.             <ref local="oracle"/>   
  76.         </set>   
  77.     </property>   
  78.     <property name="list">   
  79.         <list>   
  80.             <value>Hello World!</value>   
  81.             <ref local="oracle"/>   
  82.          </list>   
  83.     </property>   
  84. </bean>   
  85. /************************* 

猜你喜欢

转载自blog.csdn.net/jisuanji198509/article/details/80907427