spring - constructor-arg 的使用

本文介绍了 Spring 框架中 Bean 的配置与依赖注入的方法,包括属性注入、构造器注入等,并展示了如何处理构造器冲突及集合类型的注入。此外,还讲解了如何建立具有继承关系的 BeanFactory。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>   
  •       //当构造函数有多个参数时,可以使用constructor-arg标签的index属性,index属性的值从0开始。   
  1. <bean id="provider" class="com.apress.prospring.ch4.ConfigurableMessageProvider">   
  2.     <constructor-arg index="0">   
  3.         <value>first parameter</value>   
  4.     </constructor-arg>   
  5.     <constructor-arg index="1">   
  6.         <value>second parameter</value>   
  7.     </constructor-arg>   
  8.   
  9. </bean>   
  •     // 在使用构造子注入时,需要注意的问题是要避免构造子冲突的情况发生。考虑下面的情况:   
  1. public class ConstructorConfusion {   
  2.     public ConstructorConfusion(String someValue) {   
  3.         System.out.println("ConstructorConfusion(String) called");   
  4.     }   
  5.     public ConstructorConfusion(int someValue) {   
  6.         System.out.println("ConstructorConfusion(int) called");   
  7.     }   
  8. }   
  •     // 使用如下配置文件   
  1. <bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
  2.     <constructor-arg>   
  3.         <value>90</value>   
  4.     </constructor-arg>   
  5. </bean>   
  •     // 那么,当实例化组件constructorConfusion时,将输出ConstructorConfusion(String) called,也就是说参数类型为String的构造函数被调用了,这显然不符合我们的要求。为了让Spring调用参数为int的构造函数来实例化组件constructorConfusion,我们需要在配置文件中明确的告诉Spring,需要使用哪个构造函数,这需要使用constructor-arg的type属性。   
  1. <bean id="constructorConfusion" class="com.apress.prospring.ch4.ConstructorConfusion">   
  2.     <constructor-arg type="int">   
  3.         <value>90</value>   
  4.     </constructor-arg>   
  5. </bean>   
  •      //我们不仅可以构造单个BeanFactory,而且可以建立有继承关系的多个BeanFactory。只需要将父BeanFactory作为参数传给子BeanFactory的构造函数即可。   
  1. BeanFactory parent =   
  2.     new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/parent.xml"));   
  3. BeanFactory child =   
  4.     new XmlBeanFactory(new FileSystemResource("./ch4/src/conf/beans.xml"), parent);   
  5.      //如果子BeanFactory和父BeanFactory中含有名称相同的Bean,那么在子BeanFactory中使用   
  6. <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)。   
  1. <bean id="injectCollection" class="com.apress.prospring.ch4.CollectionInjection">   
  2.     <property name="map">   
  3.         <map>   
  4.             <entry key="someValue">   
  5.                 <value>Hello World!</value>   
  6.             </entry>   
  7.             <entry key="someBean">   
  8.                 <ref local="oracle"/>   
  9.              </entry>   
  10.         </map>   
  11.     </property>   
  12.     <property name="props">   
  13.         <props>   
  14.             <prop key="firstName">   
  15.                 Rob   
  16.             </prop>   
  17.             <prop key="secondName">   
  18.                 Harrop   
  19.             </prop>   
  20.         </props>   
  21.     </property>   
  22.     <property name="set">   
  23.         <set>   
  24.             <value>Hello World!</value>   
  25.             <ref local="oracle"/>   
  26.         </set>   
  27.     </property>   
  28.     <property name="list">   
  29.         <list>   
  30.             <value>Hello World!</value>   
  31.             <ref local="oracle"/>   
  32.          </list>   
  33.     </property>   
  34. </bean>   
  35. /************************* 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值