在正文中我就不多加解释了,在applicationContext.xml文件中每个注入方式都有相对应的注释,同时注意变量类型。
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--
配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理
通过bean标签配置IOC容器所管理的bean
属性:
id:设置bean的唯一标识
class:设置bean所对应类型的全类名-->
<bean id="helloworld" class="com.spring.pojo.HelloWorld"></bean>
<bean id="student" class="com.spring.pojo.Student"></bean>
<!--
第一种setter依赖注入
-->
<bean id="studentOne" class="com.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="name" value="蔡徐坤"></property>
<property name="age" value="25"></property>
<property name="gender" value="男"></property>
</bean>
<!--
第二种,构造器注入
-->
<bean id="studentTwo" class="com.spring.pojo.Student">
<constructor-arg value="1002"></constructor-arg>
<constructor-arg value="陈洁祥"></constructor-arg>
<constructor-arg value="21"></constructor-arg>
<constructor-arg value="男"></constructor-arg>
</bean>
<!--类变量注入(1)外部bean-->
<bean id="classOne" class="com.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="name" value="蔡徐坤"></property>
<property name="age" value="25"></property>
<property name="gender" value="男"></property>
<property name="clazz" ref="classTwo"></property>
</bean>
<bean id="classTwo" class="com.spring.pojo.Class_">
<property name="cid" value="1"></property>
<property name="cname" value="顶针"></property>
</bean>
<!--类变量注入(2)内部bean-->
<bean id="classThree" class="com.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="name" value="蔡徐坤"></property>
<property name="age" value="25"></property>
<property name="gender" value="男"></property>
<property name="clazz">
<bean id="classInner" class="com.spring.pojo.Class_">
<property name="cid" value="2"></property>
<property name="cname" value="王者荣耀"></property>
</bean>
<!--数组类型注入-->
</property>
<property name="hobby">
<array>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</array>
</property>
</bean>
<bean id="clazzOne" class="com.spring.pojo.Class_">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班级"></property>
<property name="students">
<list>
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="classOne"></ref>
<ref bean="classThree"></ref>
</list>
</property>
</bean>
<!--配置一个集合类型的bean,引入约束util-->
<bean id="clazzTwo" class="com.spring.pojo.Class_">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班级"></property>
<property name="students" ref="studentList"></property>
</bean>
<util:list id="studentList">
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="classOne"></ref>
<ref bean="classThree"></ref>
</util:list>
<!--下面为测试map相关的bean-->
<bean id="classFive" class="com.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="name" value="蔡徐坤"></property>
<property name="age" value="25"></property>
<property name="gender" value="男"></property>
<property name="clazz">
<bean id="classInner" class="com.spring.pojo.Class_">
<property name="cid" value="2"></property>
<property name="cname" value="王者荣耀"></property>
</bean>
</property>
<property name="hobby">
<array>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</array>
</property>
<property name="teacherMap">
<map>
<entry key="1" value-ref="teacherOne"></entry>
<entry key="2" value-ref="teacherTwo"></entry>
</map>
</property>
</bean>
<bean id="teacherOne" class="com.spring.pojo.Teacher">
<property name="tid" value="1"></property>
<property name="tname" value="陈老师"></property>
</bean>
<bean id="teacherTwo" class="com.spring.pojo.Teacher">
<property name="tid" value="2"></property>
<property name="tname" value="蔡老师"></property>
</bean>
<!--map约束-->
<bean id="classSix" class="com.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="name" value="蔡徐坤"></property>
<property name="age" value="25"></property>
<property name="gender" value="男"></property>
<property name="clazz">
<bean id="classInner" class="com.spring.pojo.Class_">
<property name="cid" value="2"></property>
<property name="cname" value="王者荣耀"></property>
</bean>
</property>
<property name="hobby">
<array>
<value>吃饭</value>
<value>睡觉</value>
<value>打豆豆</value>
</array>
</property>
<property name="teacherMap" ref="teacherMap"></property>
</bean>
<util:map id="teacherMap">
<entry key="1" value-ref="teacherOne"></entry>
<entry key="2" value-ref="teacherTwo"></entry>
</util:map>
</beans>