0

I have searched in this site a lot of spring.. but i'm really new to this.. so here is my question:

I have 3 main classes i'm gonna put it like this..

 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;
    }
 }

That's the Java code.. this is just an example. So i have 3 classes, as u see one is the user that can have one or many cars, then is the car that can have one or many wheels..

In my Spring.xml i want to do something like this..

 <?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>

If u see this spring.. i want to create the user instance and the cars and wheels by spring-code.. but it won't let me do this.. cause i can't create a ref bean with a property children.. so i can't add wheels to my ref bean car.. how can i do this? or this can't be done this way, maybe is another solution to this.. dunno.. Thanks in advance..

Sorry for my bad english.

  • 1
    Why not just set the wheels property on the original `car1` bean definition? – matts Oct 23 '12 at 15:17
  • this is just an example, but imagine that car1 can have one wheel to an user and to another user can have 2 wheels.. in that case i can't do create specifics wheels to a car, thats why i create them in the user instance.. – Gabriel Figox Vargas Fierro Oct 23 '12 at 15:21

2 Answers2

3

You can't set properties on a bean ref, but if you want to be able to reuse the same car instance with a different number of wheels for different users, you can instead make a copies of the car bean using Spring's bean definition inheritance.

In the following XML, we create an abstract car definition, then inherit from it and set the wheels property in the user definition(s).

<!-- 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>

However, you should note that a nested <bean> like above (e.g., the car bean definition is nested inside the user bean definition) is called an inner bean, and it cannot have an id or name. You also cannot refer to it from anywhere else. If you want each of your cars to have a bean id, you'd have to make them all top-level beans:

<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>
matts
  • 6,738
  • 1
  • 33
  • 50
1

You are creating a Car bean in:

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

For your purpose, I think you should define here the Wheel objects you want, and later just put a reference to this object, Something like:

<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>
greuze
  • 4,250
  • 5
  • 43
  • 62
  • Thanks for the answer.. but as i said to matts.. "this is just an example, but imagine that car1 can have one wheel to an user and to another user can have 2 wheels.. in that case i can't do create specifics wheels to a car, thats why i create them in the user instance.." I know i can do that the way you describe but i don't want to specify the wheels in the car instance.. cause it can change from one user to another.. that's why i thought i could created in the user instance.. so that way it would be "dinamyc" – Gabriel Figox Vargas Fierro Oct 23 '12 at 15:25
  • You create the wheel in ... In car1 bean definition there are only a reference to that bean (reference could be shared in other Car bean). – greuze Oct 23 '12 at 15:57