Alexandru Antochi
Alexandru Antochi

Reputation: 1465

Autowire not working in Spring

I have the following class:

public class Triangle implements Shape {
    /*@Autowired
     @Qualifier("A")*/

    private Point A;
    /*@Autowired
     @Qualifier("B")*/
    private Point B;
    /*@Autowired
     @Qualifier("C")*/
    private Point C;

    private static AtomicInteger id = new AtomicInteger();

    private int ownId;
    private int beanAttribute;
    @SuppressWarnings("unused")
    private int secondaryId;
    private String constructor = "Simple type --";

    private static final String NAME = "triangle";

    Triangle() {
        System.out.println("Triangle created without a secondaryId \n");
        secondaryId = -1;

    }

    Triangle(int secondaryId) {
        this.secondaryId = secondaryId;
        this.ownId = id.addAndGet(1);
    }

    Triangle(int secondaryId, String constructor) {
        this.secondaryId = secondaryId;
        this.ownId = id.addAndGet(1);
        this.constructor = constructor;
    }

    public Point getA() {
        return A;
    }

    public void setA(Point a) {
        A = a;
    }

    public Point getB() {
        return B;
    }

    public void setB(Point b) {
        B = b;
    }

    public Point getC() {
        return C;
    }

    public void setC(Point c) {
        C = c;
    }

    public int getOwnId() {
        return ownId;
    }
}

With the following XML config:

<beans>
<bean id="triangleDouble" class="draw.Triangle"
     scope="prototype" autowire="byName">
    <constructor-arg index="1" value="Double type ++" />
    <constructor-arg index="0" value="0" />
    <property name="beanAttribute" value="2" />
</bean>

<bean id="triangleSimple" class="draw.Triangle"
 scope="prototype" autowire="byName">
    <constructor-arg value="1" />
    <property name="beanAttribute" value="1" />
</bean>

<bean id="A" class="draw.Point">
    <property name="x" value="1" />
    <property name="y" value="2" />
</bean>

<bean id="B" class="draw.Point">
    <property name="x" value="2" />
    <property name="y" value="3" />
</bean>

<bean id="C" class="draw.Point">
    <property name="x" value="3" />
    <property name="y" value="4" />
</bean>

</beans>

The problem is that Autowiring is not working. If I set <property name="A" ref="A"/> etc, there will be no NullPointerException and I can access Point.x or Point.y, so the code is written correctly, but if I autowire byName, the autowiring will not work and when I will try to access Point.x from Triangle I get a java.lang.NullPointerException.

I tried to set the class members A,B,C with @Autowire, both with @Autowire and @Qualifier("A") and with no Annotation at all, but still it doesn't work.

Upvotes: 1

Views: 594

Answers (2)

Alexander
Alexander

Reputation: 2898

Rename your Point bean names to lower-case and add setters like 'setA()', 'setB' etc. to the Triangle class. It will solve the problem.

The trouble is that you did not follow the JavaBeans convention.

Upvotes: 1

Unknown
Unknown

Reputation: 2137

The error after running your program:

Bean property 'beanAttribute' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

<context:annotation-config />
<bean id="triangleDouble" bean id="triangleDouble" class="draw.Triangle"
         scope="prototype" autowire="byName">
        <constructor-arg index="1" value="Double type ++" />
        <constructor-arg index="0" value="0" />
        <property name="beanAttribute" value="2" />
</bean>

You have missed setter method for beanAttribute.Add the same and it should work.

For more info refer setter-injection

Upvotes: 2

Related Questions