Spring中常用的setter注入方式

在正文中我就不多加解释了,在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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值