构造注入
public class User1 {
private int id;
private String name;
private String password;
public User1(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "User1{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
//applicatonContext.xml
<bean id="user1" class="edu.cyl.User1">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="老二"></constructor-arg>
<constructor-arg name="password" value="123"></constructor-arg>
</bean>
setter注入
public class User2 {
private int id;
private String name;
private String password;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User2{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
//applicationContext.xml
<bean id="user2" class="edu.cyl.User2">
<property name="id" value="2"></property>
<property name="name" value="老三"></property>
<property name="password" value="123"></property>
</bean>