• 装配Spring Bean(二)


    通过XML配置装配Bean

    前面已经用过,使用XML装配Bean需要定义对应的XML,引入对应的XML模式(XSD)文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
        <!-- Spring Bean配置代码-->
    </beans>

    1.装配简易值

    <bean id="source" class="test3.Source">
            <property name="fruit" value="橙汁"/>
            <property name="sugar" value="少糖"/>
            <property name="size" value="大杯"/>
        </bean>
        <bean id="juiceMaker" class="test3.JuiceMaker">
            <property name="beverageShop" value="贡茶"/>
            <property name="source" ref="source"/>
        </bean>

    2.装配集合

    <bean id="complexAssembly" class="test.ComplexAssembly">
            <property name="id" value="1" />
            <property name="list">
                <list>
                    <value>value-list-1</value>
                    <value>value-list-2</value>
                    <value>value-list-3</value>
                </list>
            </property>
            <property name="map">
                <map>
                    <entry key="key1" value="value-key-1" />
                    <entry key="key2" value="value-key-2" />
                    <entry key="key3" value="value-key-3" />
                </map>
            </property>
            <property name="props">
                <props>
                    <prop key="prop1">value-prop-1</prop>
                    <prop key="prop2">value-prop-2</prop>
                    <prop key="prop3">value-prop-3</prop>
                </props>
            </property>
            <property name="set">
                <set>
                    <value>value-set-1</value>
                    <value>value-set-2</value>
                    <value>value-set-3</value>
                </set>
            </property>
            <property name="array">
                <array>
                    <value>value-array-1</value>
                    <value>value-array-2</value>
                    <value>value-array-3</value>
                </array>
            </property>
        </bean>

    有时候可能需要更加复杂的装载,比如一个List可以是一个系列类的对象,又如Map集合类,键可以使一个类对象,而值也可以是一个类对象,上代码:

    public class Role {
        private int id;
        private String roleName;
        private String note;
    //setter getter
    }
    public class User {
        private Long id;
        private String userName;
        private String note;
    //setter getter
    }
    public class UserRoleAssembly {
        private Long id;
        private List<Role> list;
        private Map<Role,User> map;
        private Set<Role> set;
    //setter getter
    }

    配置用户角色:

    <bean id="role1" class="test.Role">
            <property name="id" value="1" />
            <property name="roleName" value="role_name_1" />
            <property name="note" value="role_note_1" />
        </bean>
    
        <bean id="role2" class="test.Role">
            <property name="id" value="2" />
            <property name="roleName" value="role_name_2" />
            <property name="note" value="role_note_2" />
        </bean>
    
        <bean id="user1" class="test.User">
            <property name="id" value="1" />
            <property name="userName" value="user_name_1" />
            <property name="note" value="user_note_1" />
        </bean>
    
        <bean id="user2" class="test.User">
            <property name="id" value="2" />
            <property name="userName" value="user_name_2" />
            <property name="note" value="user_note_2" />
        </bean>
    
        <bean id="userRoleAssembly" class="test.UserRoleAssembly">
            <property name="id" value="1" />
            <property name="list">
                <list>
                    <ref bean="role1" />
                    <ref bean="role2" />
                </list>
            </property>
            <property name="map">
                <map>
                    <entry key-ref="role1" value-ref="user1" />
                    <entry key-ref="role2" value-ref="user2" />
                </map>
            </property>
            <property name="set">
                <set>
                    <ref bean="role1" />
                    <ref bean="role2" />
                </set>
            </property>
        </bean>

    3.命名空间装配

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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="role1" class="com.ssm.chapter10.pojo.Role" 
        c:_0="1" c:_1="role_name_1" c:_2="role_note_1" />
        <bean id="role2" class="com.ssm.chapter10.pojo.Role" 
            p:id="2" p:roleName="role_name_2" p:note="role_note_2" />
        <bean id="user1" class="com.ssm.chapter10.pojo.User" 
            p:id="1" p:userName="role_name_1" p:note="user_note_1" />
        <bean id="user2" class="com.ssm.chapter10.pojo.User" 
            p:id="2" p:userName="role_name_2" p:note="user_note_2" />
    
        <util:list id="list">
            <ref bean="role1" />
            <ref bean="role2" />
        </util:list>
    
        <util:map id="map">
            <entry key-ref="role1" value-ref="user1" />
            <entry key-ref="role2" value-ref="user2" />
        </util:map>
    
        <util:set id="set">
            <ref bean="role1" />
            <ref bean="role2" />
        </util:set>
    
        <bean id="userRoleAssembly" class="com.ssm.chapter10.pojo. UserRoleAssembly"
            p:id="1" p:list-ref="list" p:map-ref="map" p:set-ref="set" />
    </beans>
  • 相关阅读:
    数据库学习摘记 —— 关系代数和关系演算
    数据库学习摘记 —— 数据库基本概念杂记
    POJ 3130 How I Mathematician Wonder What You Are! (半平面相交)
    POJ 3311 Hie with the Pie (状压dp)
    hdu 1533 Going Home (最小费用最大流)
    bzoj 2115 Xor (线性基)
    hdu 5120 Intersection (圆环面积相交->圆面积相交)
    BZOJ 2460 元素(线性基)
    POJ 3348 Cows (凸包模板+凸包面积)
    UVA 12012 Detection of Extraterrestrial(KMP求循环节)
  • 原文地址:https://www.cnblogs.com/xc-xinxue/p/12381153.html
Copyright © 2020-2023  润新知