Spring:Bean 属性不可写或具有无效的 setter 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3930844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: Bean property not writable or has an invalid setter method
提问by Krt_Malta
I'm experimenting with Spring, I'm following the book: Spring: A developer's notebook. I'm getting this error:
我正在试验 Spring,我正在关注这本书:Spring:开发人员的笔记本。我收到此错误:
"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?"
"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 ArrayListRentABikeclass which implements RentABike:
我有一个ArrayListRentABike实现的类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.xmlis this:
而我的RentABike-context.xml是这样的:
<?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
请问有什么想法吗?非常感谢!Krt_马耳他
回答by Richard Kettelerij
You're using setter injection but don't have a setter defined for attribute storeName. Either add a setter/getter for storeNameor use constructor injection.
您正在使用 setter 注入,但没有为 attribute 定义 setter storeName。为storeName或使用构造函数注入添加 setter/getter 。
Since you already have a constructor defined that takes storeNameas input i'd say change your RentABike-context.xmlto the following:
由于您已经定义了一个将storeName输入作为输入的构造函数,因此我想将您更改RentABike-context.xml为以下内容:
<bean id="rentaBike" class="ArrayListRentABike">
<constructor-arg index="0"><value>Bruce's Bikes</value></constructor-arg>
</bean>
回答by Peter Tillemans
Since the parameter passed to the constructor will initialize the storeName, you can use the constructor-argelement to set the storeName.
由于传递给构造函数的参数将初始化storeName,因此您可以使用该constructor-arg元素来设置storeName。
<bean id="rentaBike" class="ArrayListRentABike">
<constructor-arg value="Bruce's Bikes"/>
</bean>
The constructor-argelements allow to pass parameters to the constructor (surprise, surprise) of your spring bean.
这些constructor-arg元素允许将参数传递给 spring bean 的构造函数(惊喜、惊喜)。
回答by ankit
This error occurs because of storeName not defined for value solution is in:
发生此错误的原因是未为值解决方案定义 storeName:
<bean id="rentaBike" class="ArrayListRentABike">
<property name="storeName"><value>"Bruce's Bikes"</value></property>
</bean>

