1.默认通过无参构造方法创建
<bean id="user" class="com.zhu.pojo.User"></bean>
2.指定通过索引值创建
<bean id="user" class="com.zhu.pojo.User"> <constructor-arg index="0" value="大牛"></constructor-arg> <constructor-arg index="1" value="17"></constructor-arg> </bean>
3.指定通过类创建
《注意》当有多个参数时不好传参,不建议使用。
<bean id="user" class="com.zhu.pojo.User"> <constructor-arg type="java.lang.String" value="小猪"></constructor-arg> <!-- <constructor-arg type="java.lang.Integer" value="18"></constructor-arg>--> </bean>
4.指定通过属性名创建
<bean id="user" class="com.zhu.pojo.User"> <constructor-arg name="age" value="19"></constructor-arg> <constructor-arg name="name" value="小强"></constructor-arg> </bean>
注意:IOC在spring容器(bean.xml文件)中就将所有类实例化了。