2010-10-14 10 views
9

Estoy experimentando con Spring, estoy siguiendo el libro: Spring: Un cuaderno de desarrollo. Estoy recibiendo este error:Spring: Propiedad de frijol no modificable o tiene un método de neutralizador

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

.. y estoy bastante perdido.

tengo una clase que implementa ArrayListRentABikeRentABike:

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

Y mi RentABike-context.xml es la siguiente:

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

alguna idea por favor? ¡Muchas gracias! Krt_Malta

Respuesta

10

Dado que el parámetro que se pasa al constructor inicializará el storeName, se puede utilizar el constructor-arg Elemento para configurar el storeName.

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

Los constructor-arg elementos permiten pasar parámetros al constructor (sorpresa, sorpresa) de su grano de primavera.

12

Está utilizando la inyección setter pero no tiene un setter definido para el atributo storeName. Agregue un setter/getter para storeName o use la inyección de constructor.

Como usted ya tiene un constructor definido que toma como entrada storeName yo diría que cambiar su RentABike-context.xml a lo siguiente:

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

Y si el nombre de propiedad es "myDataSource" (property name = "myDataSource"), entonces su colocador necesita ser nombrado como setMyDataSource(); no como setDataSource. –

+0

También SPRING espera getter con el mismo tipo de devolución de la propiedad como getMyDataSource(). Y ambos getter/setter deben ser de acceso de nivel público. –

0

Este error se produce debido a storeName No está definido para solución de valor está en:

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