(5)装配Bean配置(XML)

概述

  • Spring 中提供了3种方法进行配置(优先级越来越高):
  • 在XML中显示配置
  • 在Java的接口和类中实现配置
  • 隐式Bean的发现机制和自动装配原则

通过XML配置装配Bean

  • 约束:
<?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">
</beans>

装配简易值

<?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">
    <bean id="role_1" class="com.ssm.chapter9.pojo.Role">
        <property name="roleName" value="高级工程师" />
        <property name="note" value="重要人员" />
    </bean>
</beans>
  • id属性是找到的这个Bean的编号,不过它不是一个必需的属性,如果没有声明它,那么Spring 将会采用全限定名#{number}的格式生成编号。
  • 例如:如果只声明一个这样的类,而没有声明id="role_1", 那么为其生成的编号就是
    com.ssm.chapter9.pojo.Role#0 ,
    当它第二次声明没有id属性的Bean时,编号就是com.ssm.chapter9.pojo.Role#l

装配集合

  • 先创建一个POJO:
public class UserRoleAssembly {
   
   private Long id;
   private List<Role> list;
   private Properties props;
   private Map<Role, User> map;
   private Set<Role> set;
   private String[] array;
   
   /*** setter and getter ***/
}
  • 配置:
<bean id="complexAssembly" class="com.ssm.chapter10.pojo.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>
  • 如果集合元素为类对象:
<bean id="userRoleAssembly" class="com.ssm.chapter10.pojo.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>

命名空间装配

  • 约束:
<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">
</beans>
  • 普通配置:
    <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" />
  • c名称空间是第xx个参数
  • p名称空间是参数名称
  • 配置集合:
    <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" />
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容