装配Spring Bean

Posted 来福啊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装配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>

以上是关于装配Spring Bean的主要内容,如果未能解决你的问题,请参考以下文章

Spring_Spring与IoC_Bean的装配

Spring装配之——JAVA代码装配Bean

Spring学习笔记之装配Bean

Spring学习系列 通过Java代码装配Bean

Spring 实战-第二章-装配Bean

Spring实战读书笔记Spring装配Bean