Ancient.Square
Ancient.Square

Reputation: 53

Why does Spring obviously merges two different beans?

I am quite new to Spring and atm checking out XML-driven Spring.

Now my problem is that Spring somehow merges two different bean-objects coming from the same bean-class. It treats them as one single object.

Please, take a look at the following code:

<bean id="shapeList1" class="coreservlets.ShapeListMaker" >
    <constructor-arg index="0">
        <list>
        <ref local="shape1" /> 
        </list>
    </constructor-arg>
</bean>
<bean id="shapeList2" class="coreservlets.ShapeListMaker"  >
    <constructor-arg>
        <ref local="shape2" />
    </constructor-arg>
</bean>

...the related class:

public class ShapeListMaker {

public ShapeListMaker (List<Shape> shapes) {
    shapelist = shapes;
}

public ShapeListMaker (Shape shape) {
    shapelist.add(shape);
}

private static List<Shape> shapelist = new ArrayList<>();

Comparing both objects (shapeList1 == shapeList2) returns true. Printing out one of the lists produces both shapes, shape1 and shape2... Why? I am confused...

Via the same xml-file I created some other bean objects in the same way (the shape beans) without any trouble or merging.

Upvotes: 0

Views: 55

Answers (1)

Michael Wiles
Michael Wiles

Reputation: 21186

shapeList is static ;) so the reference is shared and will be the same on both classes.

Upvotes: 1

Related Questions