Krt_Malta
Krt_Malta

Reputation: 9465

Spring: Bean property not writable or has an invalid setter method

I'm experimenting with Spring, I'm following the book: Spring: A developer's notebook. I'm getting this error:

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

.. and I'm quite lost.

I have an ArrayListRentABike class which implements RentABike:

import java.util.*;

public class ArrayListRentABike implements RentABike {
    private String storeName;
    final List bikes = new ArrayList( );

    public ArrayListRentABike( ) { initBikes( ); }

    public ArrayListRentABike(String storeName) {
        this.storeName = storeName;
        initBikes( );
}

public void initBikes( ) {
    bikes.add(new Bike("Shimano", "Roadmaster", 20, "11111", 15, "Fair"));
    bikes.add(new Bike("Cannondale", "F2000 XTR", 18, "22222", 12, "Excellent"));
    bikes.add(new Bike("Trek", "6000", 19, "33333", 12.4, "Fair"));
}

public String toString( ) { return "RentABike: " + storeName; }

public List getBikes( ) { return bikes; }

public Bike getBike(String serialNo) {
    Iterator iter = bikes.iterator( );
    while(iter.hasNext( )) {
        Bike bike = (Bike)iter.next( );
        if(serialNo.equals(bike.getSerialNo( ))) return bike;
    }
        return null;
    }
}

And my RentABike-context.xml is this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="rentaBike" class="ArrayListRentABike">
        <property name="storeName"><value>"Bruce's Bikes"</value></property>
    </bean>

    <bean id="commandLineView" class="CommandLineView">
        <property name="rentaBike"><ref bean="rentaBike"/></property>
    </bean>

</beans>

Any ideas please? Thanks a lot! Krt_Malta

Upvotes: 11

Views: 71289

Answers (4)

Shashank Goell
Shashank Goell

Reputation: 41

on this type of error some time access modifier of the field is also required to check, by chance if it is default the spring will throw the error. access modifier of the setter should be public so that spring can use it to set the property

Upvotes: 0

ankit
ankit

Reputation: 2845

This error occurs because of storeName not defined for value solution is in:

<bean id="rentaBike" class="ArrayListRentABike">
    <property name="storeName"><value>"Bruce's Bikes"</value></property>
</bean>

Upvotes: 0

Peter Tillemans
Peter Tillemans

Reputation: 35331

Since the parameter passed to the constructor will initialize the storeName, you can use the constructor-arg element to set the storeName.

<bean id="rentaBike" class="ArrayListRentABike">
    <constructor-arg  value="Bruce's Bikes"/>
</bean>

The constructor-arg elements allow to pass parameters to the constructor (surprise, surprise) of your spring bean.

Upvotes: 10

Richard Kettelerij
Richard Kettelerij

Reputation: 2174

You're using setter injection but don't have a setter defined for attribute storeName. Either add a setter/getter for storeName or use constructor injection.

Since you already have a constructor defined that takes storeName as input i'd say change your RentABike-context.xml to the following:

<bean id="rentaBike" class="ArrayListRentABike">
    <constructor-arg index="0"><value>Bruce's Bikes</value></constructor-arg>
</bean>

Upvotes: 12

Related Questions