1.构造方法注入
在xml配置文件中
2.Set注入
在Java代码中
在实体类中必须有set方法
3.复杂类型注入
数组型数据
数组类型
<bean id="emp" class="com.atguigu.spring6.iocxml.ditest.Emp"> <!--普通属性--> <property name="ename" value="lucy"></property> <property name="age" value="20"></property> <!--对象类型属性--> <property name="dept" ref="dept"></property> <!--数组类型属性--> <property name="loves"> <array> <value>吃饭</value> <value>睡觉</value> <value>敲代码</value> </array> </property>
list 类型
<bean id="dept" class="com.atguigu.spring6.iocxml.ditest.Dept">
<property name="dname" value="技术部"></property>
<property name="empList">
<list>
<ref bean="empone"></ref>
<ref bean="emptwo"></ref>
</list>
</property>
</bean>
map类型
<bean id="teacherone" class="com.atguigu.spring6.iocxml.dimap.Teacher">
<!--注入普通类型属性-->
<property name="teacherId" value="100"></property>
<property name="teacherName" value="西门讲师"></property>
</bean>
<bean id="teachertwo" class="com.atguigu.spring6.iocxml.dimap.Teacher">
<!--注入普通类型属性-->
<property name="teacherId" value="200"></property>
<property name="teacherName" value="上官讲师"></property>
</bean>
<bean id="student" class="com.atguigu.spring6.iocxml.dimap.Student">
<!--注入普通类型属性-->
<property name="sid" value="2000"></property>
<property name="sname" value="张三"></property>
<!--在学生bean注入map集合类型属性-->
<property name="teacherMap">
<map>
<entry>
<key>
<value>10010</value>
</key>
<ref bean="teacherone"></ref>
</entry>
<entry>
<key>
<value>10086</value>
</key>
<ref bean="teachertwo"></ref>
</entry>
</map>
</property>
</bean>
p命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentp" class="com.atguigu.spring6.iocxml.dimap.Student"
p:sid="100" p:sname="mary" p:lessonList-ref="lessonList" p:teacherMap-ref="teacherMap">
</bean>
部属性文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>