Spring基本类型注入

版本:spring-framework-2.5.6
类:
public class ExampleBean {
private int integerProperty;
private int doubleProperty;
public int getIntegerProperty() {
return integerProperty;
}
public void setIntegerProperty(int integerProperty) {
this.integerProperty = integerProperty;
}
public int getDoubleProperty() {
return doubleProperty;
}
public void setDoubleProperty(int doubleProperty) {
this.doubleProperty = doubleProperty;
}
}
配置文件:


1


2.2


测试代码:
FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext(new String[]{"src/beans.xml"});
BeanFactory factory = (BeanFactory) appContext;
ExampleBean b1 = (ExampleBean) factory.getBean("exampleBean");
System.out.println(b1.getIntegerProperty()+"——"+b1.getDoubleProperty());
出错:
Caused by: java.lang.NumberFormatException: For input string: "2.2"

你的doubleProperty是int类型,转换当然要出错了。