- 有参构造器注入
<!--有参构造器注入-->
<bean id="cla" class="com.zxw.spring1.Clazz">
<constructor-arg name="cno" value="1" type="int" index="0"></constructor-arg>
<constructor-arg name="cname" value="java班" type="java.lang.String" index="1"></constructor-arg>
</bean>
<!--
value:属性类型是基本数据类型
reg:属性类型是对象
-->
<bean id="stu" class="com.zxw.spring1.Student">
<constructor-arg name="sno" value="1"></constructor-arg>
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="sex" value="女"></constructor-arg>
<constructor-arg name="clazz" ref="cla"></constructor-arg>
</bean>
- set方法注入
<!--set注入-->
<bean id="stu" class="com.zxw.spring1.Student">
<property name="sno" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="sex" value="女"></property>
<property name="clazz" ref="cla"></property>
</bean>
- 自动注入
<!--自动注入-->
<!--
autowire="xxx"
byType:底层是set注入,在当前xml中寻找【bean标签的类型】和需要注入实体中的【属性类型】一致,进行匹配
byName:底层是set注入,在当前xml中寻找【bean的id名称】和需要注入实体中的【属性名】一致,进行匹配
constructor:底层是有参构造注入
!!要有合适的构造器
该例中学生实体类中要有只有一个Clazz类型的参数的构造器
-->
<bean id="stu" class="com.zxw.spring1.Student" autowire="byName"></bean>
<bean id="stu" class="com.zxw.spring1.Student" autowire="byType"></bean>
<bean id="stu" class="com.zxw.spring1.Student" autowire="constructor"></bean>
- 特殊集合注入
<!--特殊集合注入-->
<!--数组-->
<bean id="tea" class="com.zxw.spring1.Teacher">
<property name="array">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
</bean>
<!--list-->
<bean id="tea" class="com.zxw.spring1.Teacher">
<property name="list">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
</list>
</property>
</bean>
<!--set-->
<bean id="tea" class="com.zxw.spring1.Teacher">
<property name="set">
<set>
<value>a</value>
<value>a</value>
<value>b</value>
<value>c</value>
</set>
</property>
</bean>
<!--map-->
<bean id="tea" class="com.zxw.spring1.Teacher">
<property name="map">
<map>
<entry>
<key><value>a</value></key>
<value>1</value>
</entry>
<entry>
<key><value>b</value></key>
<value>1</value>
</entry>
</map>
</property>
</bean>