1.使用无参构造器实例化,这要求bean必须要有无参构造器
<bean id="user" class="com.User"></bean>
2.使用静态工厂
<bean id="user2" class="com.UserFactory" factory-method="getUser">
<property name="id" value="1"></property>
<property name="name" value="zs"></property>
</bean>
3.使用实例工厂
<bean id="uf" class="com.UserFactory"></bean>
<bean id="user3" factory-bean="uf" factory-method="getUser2">
<property name="id" value="2"></property>
<property name="name" value="ls"></property>
</bean>
user类
public class User {
private Integer id;
private String name;
private Date birthday;
private Double money;
public User() {
}
public User(Integer id, String name, Date birthday, Double money) {
super();
this.id = id;
this.name = name;
this.birthday = birthday;
this.money = money;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthday=" + birthday + ", money=" + money + "]";
}
}
UserFactory类
public class UserFactory {
public static User getUser() {
return new User();
}
public User getUser2() {
return new User();
}
}