2010-10-14 12 views
9

Sto sperimentando con Spring, sto seguendo il libro: Spring: un notebook per sviluppatori. Sto ottenendo questo errore:Spring: proprietà Bean non scrivibile o ha un metodo setter non valido

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

.. e sto abbastanza perduto.

devo una classe che 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; 
    } 
} 

E il mio RentABike-context.xml è questo:

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

Tutte le idee per favore? Grazie mille! Krt_Malta

risposta

10

Dal momento che il parametro passato al costruttore inizializza la storeName, è possibile utilizzare il constructor-arg elemento per impostare il storeName.

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

Gli constructor-arg elementi permettono di passare i parametri al costruttore (sorpresa, sorpresa) del fagiolo di primavera.

12

Si sta utilizzando l'iniezione setter ma non è stato definito un setter per l'attributo storeName. Aggiungere un setter/getter per storeName o utilizzare l'iniezione del costruttore.

Dal momento che si dispone già di un costruttore definito che prende come input storeName direi cambiare la vostra RentABike-context.xml al seguente:

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

E se il nome proprietà è "MyDataSource" (property name = "MyDataSource"), allora la vostra setter ha bisogno di essere nominato come setMyDataSource(); non come setDataSource. –

+0

Anche SPRING si aspetta getter con lo stesso tipo di ritorno della proprietà come getMyDataSource(). E sia getter/setter dovrebbero essere l'accessibilità a livello pubblico. –

0

Questo errore si verifica a causa di storeName non definiti per soluzione valore è:

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