我正在试验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?"
..而且我很失落.
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
而我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>
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?非常感谢!Krt_Malta
Ric*_*rij 12
您正在使用setter注入,但没有为属性定义setter storeName.添加setter/getter storeName或使用构造函数注入.
由于您已经定义了一个构造函数storeName作为输入,我会说改为RentABike-context.xml以下内容:
<bean id="rentaBike" class="ArrayListRentABike">
<constructor-arg index="0"><value>Bruce's Bikes</value></constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
Pet*_*ans 10
由于传递给构造函数的参数将初始化storeName,您可以使用该constructor-arg元素来设置storeName.
<bean id="rentaBike" class="ArrayListRentABike">
<constructor-arg value="Bruce's Bikes"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
该constructor-arg元件允许传递参数给你的Spring bean的构造函数(惊讶,意外).
| 归档时间: |
|
| 查看次数: |
65398 次 |
| 最近记录: |