• Spring IOC容器基于配置文件装配Bean(3) ------装配集合属性


    <?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"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/util
                http://www.springframework.org/schema/util/spring-util.xsd ">
        <!--配置bean
        id:给配置的类起个后续在容器中获取用的id
        class:类所在的路径,通过反射的方式在IOC容器中创建bean,要求bean必须有无参构造方法
        -->
        <!--1、属性注入,通过setter方法注入,开发中最常用的注入方式-->
        <bean id="user" class="com.spring.cn.config.collections.UserBean">
            <property name="username" value="liangd"/>
            <property name="age" value="23"/>
        </bean>
    
        <!--2、通过构造器方法注入-->
        <bean id="product5" class="com.spring.cn.config.collections.ProductBean">
            <!--  <constructor-arg value="36" index="2"/>
              <constructor-arg value="南极人" index="0"/>
              <constructor-arg value="保暖" index="1"/>-->
            <constructor-arg value="365" type="double"/>
            <constructor-arg value="保暖衣" type="java.lang.String"/>
            <constructor-arg value="保暖" type="java.lang.String"/>
        </bean>
        <!--如果IOC容器中有两个一样的bean,在获取bean时不能用Class来获取-->
        <!--不加参数根据构造器参数的顺序来赋值
            1、value  赋值
            2、index  参数下标
            3、type  类型
            4、name  名称
            5、ref  引用类型可以用ref,例如UserService,基本数据类型可以用value赋值,例如int、Long等
            使用构造器的注入的属性值可以指定参数的位置和参数的类型,来区分重载的构造器
          -->
        <bean id="product6" class="com.spring.cn.config.collections.ProductBean">
            <constructor-arg value="秋裤" type="java.lang.String"/>
            <constructor-arg value="保暖" type="java.lang.String"/>
            <constructor-arg value="36" type="int"/>
        </bean>
    
        <!--value属性可以拿出来,如果有特殊字符,可以用<![CDATA[]]>处理-->
        <bean id="product7" class="com.spring.cn.config.collections.ProductBean">
            <constructor-arg type="java.lang.String">
                <value><![CDATA[<南极人…^>]]></value>
            </constructor-arg>
            <constructor-arg value="保暖" type="java.lang.String"/>
            <constructor-arg value="36" type="int"/>
        </bean>
    
    
        <!--集合测试-->
    
    
        <!--List测试-->
        <bean id="companyList" class="com.spring.cn.config.collections.CompanyListBean">
            <property name="name" value="阿里巴巴"/>
            <property name="address" value="四川成都"/>
            <property name="productBeanList">
                <!--使用list节点为list属性类型赋值, List、Set和数组赋值方式差不多-->
                <list>
                    <ref bean="product5"/>
                    <ref bean="product6"/>
                    <!--也可以用内部bean-->
                    <bean class="com.spring.cn.config.collections.ProductBean">
                        <constructor-arg type="java.lang.String">
                            <value><![CDATA[<南极人…^>]]></value>
                        </constructor-arg>
                        <constructor-arg value="保暖" type="java.lang.String"/>
                        <constructor-arg value="36" type="int"/>
                    </bean>
                </list>
                <!--<set>-->
                <!--<ref config="product5"/>-->
                <!--</set>-->
                <!--<array>-->
                <!--<ref config="product5"/>-->
                <!--</array>-->
            </property>
        </bean>
    
        <!--配置Map属性值-->
        <bean id="companyMap" class="com.spring.cn.config.collections.CompanyMapBean">
            <property name="name" value="阿里巴巴"/>
            <property name="address" value="四川成都"/>
            <property name="productBeanMap">
                <!--用map节点以及entry子节点 对Map类型的成员变量赋值-->
                <map>
                    <entry key="AA" value-ref="product5"/>
                    <entry key="BB" value-ref="product6"/>
                </map>
            </property>
        </bean>
    
        <!--通过Properties配置属性-->
        <bean id="dataSource" class="com.spring.cn.config.collections.DataSource">
            <property name="properties">
                <props>
                    <prop key="username">root</prop>
                    <prop key="password">123456</prop>
                    <prop key="url">jdbc:mysql//localhost:3306?</prop>
                    <prop key="driver">com.mysql.cj.jdbc.driver</prop>
                </props>
            </property>
        </bean>
    
    
        <!--将集合独立出来,以便其它bean调用-->
        <util:list id="productUtil">
            <ref bean="product6"/>
            <ref bean="product7"/>
        </util:list>
    
        <!--测试引用工类类赋值-->
        <bean id="companyUtil" class="com.spring.cn.config.collections.CompanyListBean">
            <property name="name" value="阿里巴巴"/>
            <property name="address" value="四川成都"/>
            <property name="productBeanList" ref="productUtil"/>
        </bean>
    
        <!--测试通过P标签赋值,需要先导包,相比于传统的配置要简单一些-->
        <bean id="companyP" class="com.spring.cn.config.collections.CompanyListBean"
              p:name="腾讯" p:address="深圳" p:productBeanList-ref="productUtil">
    
        </bean>
    </beans>
    作者:donleo123
    本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
  • 相关阅读:
    Android:简单联网获取网页代码
    nginx搭建前端项目web服务器以及利用反向代理调试远程后台接口
    ElementUI使用问题记录:设置路由+iconfont图标+自定义表单验证
    vue中引入第三方字体图标库iconfont,及iconfont引入彩色图标
    Axios使用文档总结
    使用node中的express解决vue-cli加载不到dev-server.js的问题
    Vue脚手架(vue-cli)搭建和目录结构详解
    JS夯实基础:Javascript 变态题解析 (下)
    理解JS里的稀疏数组与密集数组
    JS夯实基础:Javascript 变态题解析 (上)
  • 原文地址:https://www.cnblogs.com/donleo123/p/14069284.html
Copyright © 2020-2023  润新知