【Spring 核心】装配bean(三)XML配置

时间:2023-03-08 17:09:09

项目包结构:

src/main/java

com.bonc.pojo--|-CompactDisc.java (接口)

|-SgtPeppers.java     (实现类 实现 CompactDisc)

|-BlankDisc.java        (实现类 实现 CompactDisc)

|-MediaPlayer.java     (接口)

|-CDPlayer.java          (实现类 实现 MediaPlayer)

src/main/resources

spring.xml (Spring应用上下文配置信息)

接口 CompactDisc.java

package com.bonc.pojo;

public interface CompactDisc {
    void  play();
}

实现类 SgtPeppers.java

package com.bonc.pojo;

public class SgtPeppers implements CompactDisc {

    private String title = "Sgt. Pepper's Lonely Hearts Club Band";
    private String artist = "The Beatles";
    public void play() {
            System.out.println("Playing "+title+"by"+artist);
    }
    public SgtPeppers() {
        super();
    }
    //自定义带参构造器
    public SgtPeppers(String title, String artist) {
        super();
        this.title = title;
        this.artist = artist;
    }

}

实现类 BlankDisc.java

package com.bonc.pojo;

import java.util.List;

public class BlankDisc implements CompactDisc {

    private String title;
    private String artist;
    private List<String> tracks;
    public void play() {
        System.out.println("Playing "+title+" by "+artist);
        for(String track:tracks){
            System.out.println("-Track: "+track);
        }
    }
    //自定义带参构造器
    public BlankDisc(String title, String artist, List<String> tracks) {
        super();
        this.title = title;
        this.artist = artist;
        this.tracks = tracks;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getArtist() {
        return artist;
    }
    public void setArtist(String artist) {
        this.artist = artist;
    }
    public List<String> getTracks() {
        return tracks;
    }
    public void setTracks(List<String> tracks) {
        this.tracks = tracks;
    }

}

接口 MediaPlayer.java

package com.bonc.pojo;

public interface MediaPlayer {
    void play();
}

实现类CDPlayer.java

package com.bonc.pojo;

public class CDPlayer implements MediaPlayer{

    private CompactDisc cd;

    public CDPlayer(){
        super();
    }

    public CDPlayer(CompactDisc cd){
        this.cd = cd;
    }
    public void play() {
        cd.play();
    }

    public void setCd(CompactDisc cd) {
        this.cd = cd;
    }

}

Spring.xml配置信息

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util.xsd">

<!--
     一、XML配置说明:
    在Spring刚刚出现的时候,XML是描述配置的主要方式。
    尽管Spring长期以来确实与XML有着关联,但需要说明的是,XML不再是配置Spring的唯一方案。
    鉴于已经存在那么多基于XML的Spring配置,所以理解如何在Spring中配置XML还是很重要的。
    本篇文章在于帮助你维护已有的XML配置,在完成新的Spring工作时,希望你使用自动化配置和JavaConfig     

    如果不给出ID属性,这个bean会根据全限定类名来进行命名
    本例中为 com.bonc.pojo.SgtPeppers#0 #0是一个计数的形式,用来区分其他相同类型的bean
 -->
    <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers"/>

<!--
    二、借助构造器初始化bean有两种方案:
    1. <constructor-arg>元素
    2. 使用Spring3.0所引入的c-命名空间
-->
     <bean id="cdPlayer" class="com.bonc.pojo.CDPlayer">
         <constructor-arg  ref="compactDisc"/>
     </bean>
<!--
    c:cd-ref
    c(命名空间的前缀)-构造器的参数名-ref(告诉Spring 正在装配的是一个bean的引用)

    也可以使用参数在参数列表中的位置信息
    c:_0-ref="compactDisc"
-->
     <bean id="cdPlayer2" class="com.bonc.pojo.CDPlayer" c:cd-ref="compactDisc"/>

<!--
      装配字面量
-->
     <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers">
         <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band"/>
         <constructor-arg value="The Beatles"/>
     </bean>
     <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers"
         c:_title="gt. Pepper's Lonely Hearts Club Band"
         c:_artist="The Beatles"/>
     <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers"
         c:_0="gt. Pepper's Lonely Hearts Club Band"
         c:_1="The Beatles"/>
<!--
    装配list
-->
     <bean id="compactDisc" class="com.bonc.pojo.BlankDisc">
         <constructor-arg  value="Sgt. Pepper's Lonely Hearts Club Band"/>
         <constructor-arg  value="The Beatles"/>
         <constructor-arg>
             <list>
                 <value>Sgt.Pepper's warm heart</value>
                 <value>With a little help from My Friends</value>
                 <value>in the Sky with Diamonds</value>
                 <value>Getting Better</value>
                 <value>Fixing A Hole</value>
             </list>
         </constructor-arg>
     </bean>
<!-- 装配set -->
    <bean id="compactDisc" class="com.bonc.pojo.BlankDisc">
         <constructor-arg  value="Sgt. Pepper's Lonely Hearts Club Band"/>
         <constructor-arg  value="The Beatles"/>
         <constructor-arg>
             <set>
                 <value>Sgt.Pepper's warm heart</value>
                 <value>With a little help from My Friends</value>
                 <value>in the Sky with Diamonds</value>
                 <value>Getting Better</value>
                 <value>Fixing A Hole</value>
             </set>
         </constructor-arg>
     </bean>

<!--
    三、属性初始化bean
-->
    <bean id="compactDisc" class="com.bonc.pojo.BlankDisc">
        <property name="title" value="Sgt. Pepper's Lonely Hearts Club Band"/>
        <property name="artist"value="The Beatles"/>
        <property name="tracks">
            <list>
                <value>Sgt.Pepper's warm heart</value>
                 <value>With a little help from My Friends</value>
                 <value>in the Sky with Diamonds</value>
                 <value>Getting Better</value>
                 <value>Fixing A Hole</value>
            </list>
        </property>
    </bean>
<!--
    四、使用Spring util-命名空间简化bean配置
        首先需要在XML中声明util-命名空间及其模式
        util:list只是util-命名空间中的多个元素之一
 -->
     <util:list id="trackList">
        <value>Sgt.Pepper's warm heart</value>
        <value>With a little help from My Friends</value>
        <value>in the Sky with Diamonds</value>
        <value>Getting Better</value>
        <value>Fixing A Hole</value>
     </util:list>
     <bean id="compactDisc" class="com.bonc.pojo.BlankDisc"
         p:title="Sgt. Pepper's Lonely Hearts Club Band"
         p:artist="The Beatles"
         p:track-ref="trackList"/>

</beans>