码迷,mamicode.com
首页 > 编程语言 > 详细

装配Spring Bean(二)

时间:2020-02-29 01:04:23      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:line   cat   enc   ice   erro   str   order   pad   col   

通过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>

装配Spring Bean(二)

标签:line   cat   enc   ice   erro   str   order   pad   col   

原文地址:https://www.cnblogs.com/xc-xinxue/p/12381153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!