java中如果在一个类中没有写明任何构造函数的,那么会存在一个无参的构造函数,如下:
public class Children {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
以上代码等同于下面:
public class Children {
private String name;
private String address;
public Children() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
但如果写明了一个有参的构造函数,那么无参的构造函数就不复存在了:
所以在spring 配置的时候,以下的配置就是错误的了:
<bean id="children" class="com.wc82.Children">
<property name="address" value="stresst"></property>
<property name="name" value="nick"></property>
</bean>
上面的配置类似于:
Children c=new Children();
c.setName("nick");
c.setAddress("stresst");
但是默认的构造方法已经不存在了,所以就会报错,可以改成以下的配置:
<bean id="children" class="com.wc82.Children">
<constructor-arg value="1111" />
<constructor-arg value="" />
</bean>