zoukankan      html  css  js  c++  java
  • Spring 之通过 XML 装配 bean

    1、关于 使用传统标签还是 c- p- 命名空间定义的标签,

    我的观点是能用  c- p- 命名空间定义的标签 就不用 传统标签(这样会比较简洁。。。

    2、强依赖使用构造器注入,可选性依赖使用属性注入。

    【 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" xmlns:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    【声明简单的 bean 】

    <!-- 创建一个简单的 bean(默认构造器),自动化命名 ID 默认为 soundsystem.SgtPeppers#0 --> <bean class="soundsystem.SgtPeppers" /> <!-- 自定义 ID 非必要不使用 --> <bean id="compactDisc" class="soundsystem.SgtPeppers" />

    【借助构造器注入初始化 bean

    <!-- 构造器注入 bean 引用 方案一:<constructor-arg>元素 --> <bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc" /> </bean> <!-- 构造器注入 bean 引用 方案二:Spring 3.0 引入的 c-命名空间 需要额外加 XML 顶部声明 --> <bean id="cdPlayer#0" class="soundsystem.CDPlayer" c:cd-ref="compactDisc" /> <bean id="cdPlayer#1" class="soundsystem.CDPlayer" c:_0-ref="compactDisc" /> <!-- 构造器注入字面值 方案一 <constructor-arg>元素 --> <bean id="compactDisc#0" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> </bean> <!-- 构造器注入字面值 方案二 c-命名空间 --> <bean id="compactDisc#1" class="soundsystem.BlankDisc" c:_0="Sgt. Pepper's Lonely Hearts Club Band" c:_1="The Beatles" /> <!-- 装配集合 List Set 数组都可以使用 <list>和<set>标签 --> <bean id="compactDisc#2" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> <constructor-arg><null/></constructor-arg> </bean> <bean id="compactDisc#3" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> <constructor-arg> <list> <value>Sgt. Pepper's Lonely Hearts Club Band</value> <value>With a Little help from My Friends</value> <value>Lucy in the Sky with Diamonds</value> </list> </constructor-arg> </bean> <bean id="NameOfASong" class="java.lang.String" /> <bean id="compactDisc#4" class="soundsystem.BlankDisc"> <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band" /> <constructor-arg value="The Beatles" /> <constructor-arg> <list> <ref bean="NameOfASong" /> </list> </constructor-arg> </bean> </beans>

    【设置 bean 属性】

    类似于初始化 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" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:util="http://www.springframework.org/schema/util"
           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="compactDisc" class="soundsystem.SgtPeppers" />
        <bean id="cdPlayer#0"
              class="soundsystem.CDPlayer">
            <property name="compactDisc" ref="compactDisc" />
            <!-- 需要强调,上面的属性必须要有 Setter方法 -->
        </bean>
    
        <!-- 等价的 p 命名空间 -->
        <bean id="cdPlayer#1"
              class="soundsystem.CDPlayer"
              p:compactDisc-ref="compactDisc" />
    
        <!-- 注入字面量到属性 -->
        <bean id="reallyBlankDisc"
              class="soundsystem.BlankDisc">
            <property name="title" value="Sgt. Pepper's Lonely Hearts Club Band" />
            <property name="artist" value="The Beatles" />
            <property name="tracks">
                <list>
                    <value>...</value>
                    <value>....</value>
                </list>
            </property>
        </bean>
    
        <!-- 等价的 p 命名空间, 无 法 用 它 来 装配 集合 -->
        <bean id="reallyBlankDisc#0"
              class="soundsystem.BlankDisc"
              p:title="Sgt. Pepper's Lonely Hearts Club Band"
              p:artist="The Beatles">
            <property name="tracks">
                <value>...</value>
                <value>....</value>
            </property>
        </bean>
    
        <!-- 可取的方案 util-命令空间中的 <util:list>标签 -->
        <util:list id="trackList">
            <value>...</value>
            <value>....</value>
        </util:list>
    
        <bean id="reallyBlankDisc#2"
              class="soundsystem.BlankDisc"
              p:title="..."
              p:artist="..."
              p:tracks-ref="trackList" />
    
    </beans>
  • 相关阅读:
    如何在Ubuntu Server 18.04上安装Microsoft的Procmon
    如何在Ubuntu 20.04上安装Wine 5.0
    如何在Kali Linux 2020中启用SSH服务
    如何在Ubuntu 20.04 LTS Focal Fossa上安装Apache Groovy
    如何使用命令在Ubuntu 20.04 Linux上安装Vmware Tools
    在Ubuntu 20.04 LTS Focal Fossa上安装Zabbix Agent
    hdu 2089 不要62
    hdu 2093 成绩排名
    hdu 2104 hide handkerchief
    leetcode147对链表进行插入排序
  • 原文地址:https://www.cnblogs.com/xkxf/p/7149031.html
Copyright © 2011-2022 走看看