`
胡小米
  • 浏览: 78619 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(转载)使用constructor-arg子标签,来指定构造函数的参数

 
阅读更多
[color=violet][/color]
   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子标签,来指定组件的属性。
<bean id="renderer" class="com.apress.prospring.ch2.StandardOutMessageRenderer">
    <property name="messageProvider">
        <ref local="provider"/>
    </property>
</bean>
      使用构造子注入时,则使用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"/>引用的将是子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>
/*************************

type 的值定义的类型必须与构造器定义的类型一致,并且不支持类型的自动包装[/size]
分享到:
评论

相关推荐

    jcaptcha-all-1.0-RC6.jar 和 解决步骤 包

    &lt;constructor-arg index="0" ref="imageEngine"/&gt; &lt;constructor-arg type="int" index="1" value="180"/&gt; &lt;constructor-arg type="int" index="2" value="100000"/&gt; &lt;!--注释此处--&gt; &lt;!-- ...

    Spring自学笔记-Ioc(控制反转)容器

    也可以通过索引来指定构造函数参数的顺序: ```xml &lt;constructor-arg index="0" value="258"/&gt; &lt;constructor-arg index="1" value="XXX"/&gt; ``` 2. **Setter注入** Setter注入是最常见的依赖注入方式,...

    Spring项目bean实例化代码

    在Spring配置文件中,可以通过`&lt;bean&gt;`标签的`constructor-arg`子标签来指定构造函数参数,Spring会根据这些参数调用相应的构造函数来创建Bean实例。例如: ```xml &lt;constructor-arg value="param1"/&gt; ...

    Spring.Net教程(4)对象的创建

    这通常通过`&lt;object&gt;`标签实现,指定`type`属性为要创建的对象类型,并可以使用`constructor-arg`子元素提供构造函数参数。例如: ```xml , MyAssembly"&gt; &lt;constructor-arg index="0" value="param1"/&gt; ...

    applicationContext.xml详解

    3. 使用&lt;constructor-arg&gt;标签来配置Bean的构造函数,例如:&lt;constructor-arg&gt; 4. 使用标签来配置Bean的集合属性,例如:&lt;list&gt; &lt;value&gt;com/alonely/vo/User.hbm.xml&lt;/value&gt; &lt;/list&gt; 在ApplicationContext.xml...

    通过bean的构造器赋值.zip

    在XML配置文件中,可以定义一个bean,并使用`&lt;constructor-arg&gt;`或`&lt;constructor-ref&gt;`元素来指定构造函数参数。例如: ```xml &lt;constructor-arg value="dependencyValue1"/&gt; &lt;constructor-arg&gt; &lt;/...

    SSH配置及基础教程

    DI主要通过setter方法、构造函数和接口来实现。 1. Setter方法注入: 当一个类的成员变量是接口类型时,Spring会通过setter方法来注入其实现类。例如,如果我们有一个`UserService`接口和它的实现类`...

    Spring依赖注入的两种方式(根据实例详解)

    在上面的配置文件中,我们使用`&lt;constructor-arg&gt;`元素将三个参数传递给Car对象的构造函数。 总结 Spring依赖注入的两种方式:Set方法注入和构造注入。Set方法注入是通过对象的setter方法来完成依赖关系的设置,而...

    spring通过构造函数注入实现方法分析

    2. 在 beans.xml 文件中,使用 `&lt;constructor-arg&gt;` 元素来指定构造函数的参数值,例如 `&lt;constructor-arg index="0" type="java.lang.String" value="大明" /&gt;`。 3. 在 App1 类中,使用 ApplicationContext 对象来...

    Spring设值注入和构造注入(通过xml).zip

    Spring配置文件中使用`&lt;constructor-arg&gt;`标签来指定构造函数参数。例如: ```xml &lt;constructor-arg ref="dependencyBean"/&gt; ``` 这里,Spring会调用`ExampleBean`的带一个参数的构造函数,并传入`...

    Spring_IoC入门笔记.md

    使用的标签:constructor-arg 标签出现的位置:bean标签的内部 标签中的属性: type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些数据的类型 index:用于指定要注入的数据 给构造函数中...

    Spring-Bean创建对象的步骤方式详解

    在上面的配置中,我们使用了 `&lt;constructor-arg&gt;` 元素来指定构造函数的参数,index 属性指定了参数的索引,value 属性指定了参数的值。 通过有参构造函数创建(type 方式) 除了使用索引来指定构造函数的参数外,...

    Springjava.pdf

    这是最常见的一种注入方式,通过在配置文件中定义bean,并使用`&lt;property&gt;`标签指定需要注入的属性。例如,配置文件中如下的代码: ```xml &lt;!-- setter injection using the nested &lt;ref/&gt; element --&gt; ...

    Spring的注入方式详解

    在 Spring 中,可以通过 `&lt;constructor-arg&gt;` 标签指定构造函数参数。例如,如果 `User` 类有一个接受 `Home` 参数的构造函数,那么配置文件中会有如下代码: ```xml &lt;constructor-arg&gt; &lt;/constructor-arg&gt; ...

    04spring4_ioc3.rar

    在XML配置中,我们可以使用`constructor-arg`标签来传递参数: ```xml &lt;constructor-arg value="张三" /&gt; &lt;constructor-arg value="25" /&gt; ``` 或者在Java配置中,使用`@Autowired`注解和`@Value`注解: ```...

    spring IOC授课代码

    首先,我们需要一个带有参数的构造函数,然后在配置文件中使用`&lt;constructor-arg&gt;`标签指定构造函数的参数。例如: ```xml &lt;constructor-arg ref="dependencyBean"/&gt; ``` 这里,`exampleBean`的构造函数会接收到`...

    springXmlBean.rar

    除了这些基础属性,我们还可以在`&lt;bean&gt;`标签内添加`&lt;property&gt;`标签来设置bean的属性值,或使用`&lt;constructor-arg&gt;`来传递构造函数参数,如: ```xml &lt;constructor-arg index="0" value="constructorArg1"/&gt;...

    Spring总结.txt

    这里使用了`&lt;bean&gt;`标签定义了一个名为“car”的Bean,并通过`&lt;constructor-arg&gt;`标签指定了构造函数参数。 - **通过`&lt;property&gt;`标签设置Bean属性**:可以使用`&lt;property&gt;`标签为Bean设置属性值。例如: ```xml...

    Spring的Bean配置

    2. **构造器注入**:使用`&lt;constructor-arg&gt;`标签指定构造函数参数,或者在注解配置中使用`@Autowired`配合构造函数。 3. **setter注入**:通过setter方法注入依赖,同样可以通过XML或注解实现。 4. **接口注入**...

    j2ee架构与程序设计spring.ppt

    ```\n\n**装配构造方法参数**\n当一个Bean的构造函数有多个参数时,可以使用`&lt;constructor-arg&gt;`标签来设定参数值,通过`type`或`index`属性来指定参数的位置或类型。\n\n```xml &lt;constructor-arg index="0"&gt; ...

Global site tag (gtag.js) - Google Analytics