1.通过构造方法创建
1.1无参构造创建(默认情况)
1.2有参构造创建(需要明确配置)
<bean id="peo" class="com.test.People">
<!--ref属性引用另一个Bean value用于基本数据类型或String等赋值 -->
<constructor-arg index="0" value="12"></constructor-arg>
<constructor-arg index="1" value="ceshi"></constructor-arg>
</bean>
index="0" 表示第一个参数
name属性是按照参数名进行匹配
2.实例工厂(需要实例化工厂)
class PeopleFactory{
public People newInstance(){
return new People();
}
}
配置:
<bean id="peoFactory" class="com.test.PeopleFactory"></bean>
<bean id="peo1" factory-bean="peoFactory" factory-method="newInstance"></bean>
3.静态工厂(不需要实例化工厂)
class PeopleFactory{
public static People newInstance(){
return new People();
}
}
配置:
<bean id="peo2" class="com.test.PeopleFactory" factory-method="newInstance"></bean>
网友评论