Tôi đang thử nghiệm với Spring, tôi đang theo dõi cuốn sách: Spring: Sổ tay của nhà phát triển. Tôi gặp phải lỗi này:Mùa xuân: Thuộc tính Bean không thể ghi hoặc có phương thức setter không hợp lệ
"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?"
.. và tôi khá lạc.
Tôi có một lớp ArrayListRentABike
mà thực hiện 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;
}
}
Và RentABike-context.xml
của tôi là thế này:
<?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>
Bất kỳ ý tưởng xin vui lòng? Cảm ơn rất nhiều! Krt_Malta
Và nếu tên thuộc tính là "myDataSource" (property name = "myDataSource"), sau đó setter của bạn cần phải được đặt tên như setMyDataSource(); không giống như setDataSource. –
Cũng SPRING dự kiến getter với cùng kiểu trả về của thuộc tính như getMyDataSource(). Và cả getter/setter đều phải là cấp độ truy cập công cộng. –