【问题标题】:ref vs idref attributes in spring bean declarationspring bean声明中的ref vs idref属性
【发布时间】:2009-11-20 01:49:27
【问题描述】:

谁能告诉我两者的区别

<bean id="b1" class="" />

<bean id="" class="">
 <property name="b1" ref="b1" />
</bean>

<bean id="" class="">
 <property name="b1" idref="b1" />
</bean>

什么时候必须使用哪一个?

【问题讨论】:

    标签: spring


    【解决方案1】:

    这里有一个更详细的例子,假设你有两个 bean A 和 B:

    <bean class="A" id="a" />
    
    <bean class="B"/>
        <constructor-arg>
            <ref bean="a"/>
            <idref bean="a"/>
        </constructor-arg>
    </bean>
    

    在这种情况下,B 将有一个如下所示的构造函数:

    public B(A a, String string) {
         string.equals("a"); //true
    }
    

    所以使用 ref 你可以引用一个对象,而使用 idref 你只需引用 bean 的名称

    【讨论】:

      【解决方案2】:

      ref 用于传递 ref 所引用的 bean。
      idref 用于传递所引用的 bean 的名称(作为 String)。

      http://forum.springsource.org/showthread.php?t=74355

      【讨论】:

        【解决方案3】:

        idref 必须指向一个真正的 bean。格式应为&lt;idref bean=""/&gt;

        【讨论】:

          【解决方案4】:

          id用于创建Class的实例,idref用于指向bean的名字

          【讨论】:

          • 问清楚ref和idref的区别。
          猜你喜欢
          • 2015-06-29
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          • 2011-01-05
          • 1970-01-01
          • 2018-12-26
          • 2023-03-08
          相关资源
          最近更新 更多