1、 依赖注入三种方式
Spring容器加载配置文件之后,通过
反射
创建类的对象,并给属性赋值;Spring容器通过反射实现属性注入有三种方式:
set方法注入
构造器注入
接口注入(不常用)
2、set方法注入
在bean标签中通过配置property标签给属性属性赋值,实际上就是通过反射调用set方法完成属性的注入
简单类型及字符串
-
直接通过property标签的value属性赋值
<!--通过bean将实体类配置给Spring进行管理,id表示实体类的唯一表示-->
<bean id="stu" class="com.qfedu.ioc.bean.Student" autowire="byName">
<!-- 简单类型 -->
<property name="stuNum" value="10001"/>
<property name="stuAge" value="12"/>
<!-- 字符串类型-->
<property name="weight" value="62.3"/>
</bean>
日期类型
-
方式1:在property标签中通过ref引用Spring容器中的一个对象
<bean id="date" class="java.util.Date"></bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student" >
<!-- 日期类型-->
<property name="enterenceTime" ref="date"/>
</bean>
-
方式2:在property标签中添加子标签bean来指定对象
<bean id="stu" class="com.qfedu.ioc.bean.Student" >
<!-- 日期类型-->
<property name="enterenceTime">
<bean class="java.util.Date"/>
</property>
</bean>
自定义类对象属性
-
方式1:
<bean id="cla" class="com.qfedu.ioc.bean.Clazz">
<property name="classId" value="2010"/>
<property name="className" value="Java2010"/>
</bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student">
<!-- 自定义对象类型-->
<property name="clazz" ref="cla"/>
</bean>
-
方式2
<bean id="stu" class="com.qfedu.ioc.bean.Student">
<!-- 自定义对象类型-->
<property name="clazz">
<bean class="com.qfedu.ioc.bean.Clazz">
<property name="classId" value="2010"/>
<property name="className" value="Java2010"/>
</bean>
</property>
</bean>
集合类型
-
List
-
List<String> List中的元素是字符串或者简单类型的封装类
<property name="hobbies" value="旅游,电影"/> <property name="hobbies" > <list> <value>旅游</value> <value>电影</value> <value>Java</value> </list> </property>
-
List<Object> List中的元素是对象类型
<property name="hobbies" > <list> <bean class="com.qfedu.ioc.bean.Book"/> <bean class="com.qfedu.ioc.bean.Book"/> <bean class="com.qfedu.ioc.bean.Book"/> <bean class="com.qfedu.ioc.bean.Book"/> </list> </property> <property name="hobbies" > <list> <ref bean="book"></ref> <!--引用容器中的备案--> <ref bean="book"></ref> </list> </property>
-
-
Set
<property name="sets"> <set> <!--和list元素注入方式相同--> </set> </property>
-
Map
<property name="maps"> <map> <entry> <key> <value>k1</value> </key> <value>123</value> </entry> <entry> <key> <value>k2</value> </key> <value>456</value> </entry> </map> </property>
-
Properties
<property name="properties"> <props> <prop key="k1">aaa</prop> <prop key="k2">bbb</prop> </props> </property>
3、 构造器注入
简单类型、字符串、对象
public class Student {
private String stuNum;
private String stuName;
private String stuGender;
private int stuAge;
private double weight;
private Date enterenceTime; //入学日期
private Clazz clazz;
public Student(String stuNum, String stuName, String stuGender, int stuAge, double weight, Date enterenceTime, Clazz clazz) {
this.stuNum = stuNum;
this.stuName = stuName;
this.stuGender = stuGender;
this.stuAge = stuAge;
this.weight = weight;
this.enterenceTime = enterenceTime;
this.clazz = clazz;
}
}
<bean id="date" class="java.util.Date"></bean>
<bean id="stu" class="com.qfedu.ioc.bean.Student">
<constructor-arg index="0" value="10001"/> <!--字符串类型-->
<constructor-arg index="2" value="女"/>
<constructor-arg index="1" value="张三"/>
<constructor-arg index="3" value="21"/> <!--简单类型-->
<constructor-arg index="4" value="62.5"/>
<constructor-arg index="5" ref="date"/> <!--对象类型-->
<constructor-arg index="6"> <!--对象类型-->
<bean class="com.qfedu.ioc.bean.Clazz"></bean>
</constructor-arg>
</bean>
集合类型属性
public class Student{
private List<String> hobbies;
private Set<String> sets;
private Map<String,Object> maps;
private Properties properties;
public Student(List<String> hobbies, Set<String> sets, Map<String, Object> maps, Properties properties) {
this.hobbies = hobbies;
this.sets = sets;
this.maps = maps;
this.properties = properties;
}
}
<bean id="stu1" class="com.qfedu.ioc.bean.Student">
<constructor-arg index="0">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</constructor-arg>
<constructor-arg index="1">
<set>
<value>aa</value>
<value>bb</value>
<value>cc</value>
</set>
</constructor-arg>
<constructor-arg index="2">
<map>
<entry>
<key><value>key1</value></key>
<value>value1</value>
</entry>
<entry>
<key><value>key2</value></key>
<value>value2</value>
</entry>
</map>
</constructor-arg>
<constructor-arg index="3">
<props>
<prop key="k1">v1</prop>
<prop key="k2">v2</prop>
</props>
</constructor-arg>
</bean>