1

I am new to Spring MVC.I am learning the concept of dependency Injection.I am following this link for the code example of Spring using dependency Injection.

Spring Example

In that example i was very clear of the concept of dependency Injection. But i have a small question on how to tell Spring that i want to use multiple shapes. In that First example (Constructor based) he gave a reference to a Circle Object so that it draws a circle.

<bean id="geometryExample1" class="com.boraji.tutorail.spring.GeometryExample1">
    <constructor-arg ref="circleShape"/>
</bean>

But what if want to draw both Circle,Rectangle and other Shapes? how do i tell or configure in Spring that depending on what shape i have provided it should use the approriate shape to draw the shape.

Any help is appreciated. Thanks in advance. Any suggestions?

Stack
  • 33
  • 1
  • 6
  • Your question has nothing to do with Spring. The logic how to draw a shape is encapsulated in each of its subclasses. `GeometryExample1` does not and should not know how to draw a shape, it only calls the `draw` method that has different implementation based on the `Shape` instance passed to it. – tsolakp Dec 13 '17 at 16:53

2 Answers2

1

Please find a tutorial that uses Java config instead of XML config. Your life will be much easier if you learn to use Java config.

When autowiring you can specify a @Qualifier and reference the bean by the id, e.g.

// This is your circle object
@Autowired
@Qualifier("geometryExample1")
public GeometryExample1 circleShape;

If you had

<bean id="squareExample" class="com.boraji.tutorail.spring.GeometryExample1">
    <constructor-arg ref="squareShape"/>
</bean>

...then in your code you'd have this:

// This is your square object
@Autowired
@Qualifier("squareExample")
public GeometryExample1 squareShape;

See How to autowire by name in Spring with annotations? for a little more detail and an example of instantiating a bean using Java config.

Paul
  • 19,704
  • 14
  • 78
  • 96
  • Hi @Paul thanks a lot for the suggestion. i will learn annotation based configuration too. But Can you please help how can i pass Multiple shapes here instead of one please? – Stack Dec 13 '17 at 18:46
  • also how can i configure in spring in sucha way that it accepts multiple shapes? @Paul – Stack Dec 13 '17 at 20:01
  • The answer by @KamilNoster seems complete - please try that and comment if it doesn't work. – Paul Dec 14 '17 at 14:54
0

Implement your classes:

class CircleShape implements Shape {
    void draw() {
        // TODO implementation
    }
}

class RectangleShape implements Shape {
    void draw() {
        // TODO implementation
    }
}

Declare beans:

<bean id="rectangleShape" class="com.boraji.tutorail.spring.RectangleShape" />
<bean id="rectangleShape" class="com.boraji.tutorail.spring.CircleShape" />

<bean id="geometryExample1" class="com.boraji.tutorail.spring.GeometryExample1">
    <constructor-arg ref="rectangleShape"/>
</bean>

Java methods are virtual, so draw() from RectangleShape is called. Change geometryExample1 bean constructor arg back to CircleShape:

<bean id="geometryExample1" class="com.boraji.tutorail.spring.GeometryExample1">
    <constructor-arg ref="circleShape"/>
</bean>

Now draw() from CircleShape is called.

Read more about virtual methods here. And use annotations, please.

I hope I understood your problem correctly.

EDIT: Example class:

class GeometryExample1 {
    private Set<Shape> shapes;

    void example() {
        shapes.foreach(Shape::draw);
    }

    public void setShapes(Set<Shape> shapes) {
         this.shapes = shapes;
    }

    public Set<Shape> getShapes() {
        return shapes;
    }
 }

And bean declaration:

<bean id="geometryExample1" class="com.boraji.tutorail.spring.GeometryExample1">
    <property name="shapes">
        <set>
            <ref bean="circleShape" />
            <ref bean="rectangleShape" />
        </set>
    </property>
</bean>

In this case, annotation config should be way more clear.

  • Thanks. But what if i want to draw a circle and rectangle one after other? We can not go back and change the configuration for each shape . So Can you please exaplain how will be the configuration and using them ? – Stack Dec 13 '17 at 17:00
  • Thanks for the answer. But there can be special scenario's right? Like What if i just want to draw rectangle once and both rectangle and circle once? So i should make changes in this case in cnfiguration. So is there a dynamic way just to pass it to the Shape object and can pass multiple shapes to the same object ? – Stack Dec 13 '17 at 18:45