0

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

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

 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>

如果你看到这个春天..我想通过弹簧代码创建用户实例以及汽车和车轮..但它不会让我这样做..因为我无法创建具有属性 children 的 ref bean。 . 所以我不能在我的 ref bean 车上加轮子.. 我该怎么做?或者这不能这样做,也许是另一种解决方案..不知道..提前谢谢..

对不起,我的英语不好。

4

2 回答 2

3

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

在下面的 XML 中,我们创建了一个抽象的汽车定义,然后从它继承并在用户定义中设置轮子属性。

<!-- 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,它不能有idname。您也无法从其他任何地方引用它。如果你想让你的每辆车都有一个 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>
于 2012-10-23T15:37:32.837 回答
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>
于 2012-10-23T15:20:30.393 回答