为ref bean创建属性,Spring

时间:2012-10-23 15:02:04

标签: java spring properties javabeans ref

我在这个网站上搜索了很多春天..但我真的很新... 所以这是我的问题:

我有三个主要课程,我会这样说..

 public class User
 {
    private String name;
    private ArrayList<Car> cars;

    public ArrayList<Car> getCars() 
    {
       return cars;
    }

    public void setCars(ArrayList<Car> cars) 
    {
       this.cars= cars;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
       this.name = name;
    }
 }

 public class Car
 {
    private String type;
    private ArrayList<Wheel> wheels;

    public ArrayList<Wheel> getWheels() 
    {
       return wheels;
    }

    public void setWheels(ArrayList<Wheel> wheels) 
    {
       this.wheels= wheels;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
       this.type= type;
    }
 }

 public class Wheel
 {
    private String name;        

    public String getName() {
        return name;
    }

    public void setType(String name) {
       this.name= name;
    }
 }

这是Java代码..这只是一个例子。所以我有3个班级,因为你看到一个是可以拥有一辆或多辆汽车的用户,那么这辆汽车可以有一个或多个车轮..

在我的Spring.xml中,我想做这样的事情......

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="car1" class="Car">
        <property name="type" value="motorcycle"/>
    </bean>

    <bean id="wheel1" class="Wheel">
        <property name="name" value="one"/>
    </bean>
    <bean id="wheel2" class="Wheel">
        <property name="name" value="two"/>
    </bean>

    <bean id="user1" class="User">
        <property name="name" value="Me"/>
        <property name="cars">
            <list value-type="Car">
                <ref bean="car1">
                  <property name="wheels">
                     <ref bean="wheel1">
                     <ref bean="wheel2">
                  </property">
                </ref>
            </list> 
        </property>
    </bean>

如果你看到这个春天..我想通过spring-code创建用户实例和汽车和轮子..但它不会让我这样做...因为我无法创建一个带有一个ref bean属性孩子..所以我不能给我的ref豆车添加轮子..我怎么能这样做?或者这不可能这样做,也许是另一个解决方案... dunno ..提前谢谢..

抱歉我的英语不好。

2 个答案:

答案 0 :(得分:3)

您不能在bean ref上设置属性,但如果您希望能够为不同的用户重复使用具有不同轮数的同一汽车实例,则可以使用Spring的bean制作汽车bean的副本定义继承。

在以下XML中,我们创建一个抽象汽车定义,然后从中继承并在用户定义中设置wheels属性。

<!-- note that original car bean is now marked abstract -->
<bean id="carPrototype" class="Car" abstract="true">
    <property name="type" value="motorcycle"/>
</bean>

<bean id="wheel1" class="Wheel">
    <property name="name" value="one"/>
</bean>
<bean id="wheel2" class="Wheel">
    <property name="name" value="two"/>
</bean>

<bean id="user1" class="User">
    <property name="name" value="Me"/>
    <property name="cars">
        <list value-type="Car">
            <!-- note the use of 'parent' attribute -->
            <bean parent="carPrototype">
                <!-- the type property of "motorcycle" is inherited from the carPrototype definition -->
                <property name="wheels">
                    <ref bean="wheel1">
                    <ref bean="wheel2">
                </property">
            </bean>
        </list> 
    </property>
</bean>

<bean id="user2" class="User">
    <property name="name" value="You"/>
    <property name="cars">
        <list value-type="Car">
            <bean parent="carPrototype">
                <!-- the type property of "motorcycle" is inherited from the carPrototype definition -->
                <property name="wheels">
                    <ref bean="wheel1">
                </property">
            </bean>
        </list> 
    </property>
</bean>

但是,您应该注意到上面嵌套的<bean>(例如,汽车bean定义嵌套在用户bean定义中)被称为内部 bean,它不能有 id 名称。您也无法从其他任何地方引用它。如果你想让你的每辆汽车都有一个bean id,你必须把它们变成所有顶级bean:

<bean id="carPrototype" class="Car" abstract="true">
    <property name="type" value="motorcycle"/>
</bean>

<bean id="car1" parent="carPrototype">
    <!-- the type property of "motorcycle" is inherited from the carPrototype definition -->
    <property name="wheels">
        <ref bean="wheel1">
        <ref bean="wheel2">
    </property">
</bean>

<bean id="car2" parent="carPrototype">
    <!-- the type property of "motorcycle" is inherited from the carPrototype definition -->
    <property name="wheels">
        <ref bean="wheel1">
    </property">
</bean>

<bean id="wheel1" class="Wheel">
    <property name="name" value="one"/>
</bean>
<bean id="wheel2" class="Wheel">
    <property name="name" value="two"/>
</bean>

<bean id="user1" class="User">
    <property name="name" value="Me"/>
    <property name="cars">
        <list value-type="Car">
            <ref bean="car1"/>
        </list> 
    </property>
</bean>

<bean id="user2" class="User">
    <property name="name" value="You"/>
    <property name="cars">
        <list value-type="Car">
            <ref bean="car2"/>
        </list> 
    </property>
</bean>

答案 1 :(得分:1)

您正在创建一个Car bean:

<bean id="car1" class="Car">
    <property name="type" value="motorcycle"/>
</bean>

出于您的目的,我认为您应该在这里定义您想要的Wheel对象,稍后只需要对此对象进行引用,类似于:

<bean id="car1" class="Car">
    <property name="type" value="motorcycle"/>
    <property name="wheels">
        <list>
            <ref bean="wheel1">
            <ref bean="wheel2">
        </list>
    </property">
</bean>

<bean id="wheel1" class="Wheel">
    <property name="name" value="one"/>
</bean>
<bean id="wheel2" class="Wheel">
    <property name="name" value="two"/>
</bean>

<bean id="user1" class="User">
    <property name="name" value="Me"/>
    <property name="cars" ref="car1"/>
</bean>