项目包结构:

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>

【Spring 核心】装配bean(三)XML配置的更多相关文章

  1. Spring_总结_03_装配Bean(三)_XML配置

    一.前言 本文承接上一节:Spring_总结_03_装配Bean(二)之Java配置 上一节说到,当需要显示配置时,首选类型安全并且比XML更强大Java配置. 那什么时候使用XML配置呢? (1)维 ...

  2. IoC容器装配Bean(xml配置方式)(Bean的生命周期)

    1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...

  3. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  4. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  5. MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  6. Spring声明式事务(xml配置事务方式)

    Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...

  7. Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire

    创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...

  8. Spring装配Bean---使用xml配置

    声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...

  9. Spring容器装配Bean的三种方式

    欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...

  10. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

随机推荐

  1. spring boot controller路由 url 扫描不到问题

    spring boot项目出现controller的路由没被注册,原因:启动类application跟controller不在一个包中,扫描不到controller, 如启动类在com.oyx.a,c ...

  2. POJ 3279 枚举(思维)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10931   Accepted: 4029 Descrip ...

  3. MFC常见问题以及解决方法(1)_MFC下文本编辑框按下回车后窗口退出

    这里主要介绍遇到这种方法的解决方案,解决方法可能有多种,但这里只给出有效的一种,这里不会详细说明出现问题的原因以及为什么这样解决,想了解更多可以百度,写这个主要是防止以后忘记,做个简单的笔记. 问题: ...

  4. Android项目导入工程Module

    在Android开发过程中,我们经常引用一些模块,或者自己封装好的Project.在Android Studio某个项目是可以引入多个Module的.这样导入Module的好处方便对源码修改以适合自己 ...

  5. NOIP模拟:切蛋糕(数学欧拉函数)

    题目描述  BG 有一块细长的蛋糕,长度为 n. 有一些人要来 BG 家里吃蛋糕, BG 把蛋糕切成了若干块(整数长度),然后分给这些人. 为了公平,每个人得到的蛋糕长度和必须相等,且必须是连续的一段 ...

  6. UWP:使用Behavior实现Button点击动态效果

    废话不多说,先上效果 没有做成安卓那种圆形的原因是...人家真的不会嘛... 好了下面是正文: 首先在工程中引入Behavior的库,我们使用Nuget. 在项目->引用上点击右键,点击管理Nu ...

  7. 机器学习(3)-Tensorflow安装与测试

    安装.# Ubuntu/Linux 64-bit $ sudo apt-get install python-pip python-dev # Ubuntu/Linux 64-bit, CPU onl ...

  8. Hbase单机安装部署

    Hbase单机安装部署 http://blogxinxiucan.sh1.newtouch.com/2017/07/27/Hbase单机安装部署/ 下载Hbase Hbase官网下载地址 http:/ ...

  9. 从Ubunt的安装到hadoop集群的搭建

    一.相关基础配置 1.网络设置 a.调整VMnet8这块网卡网关 b.在VMware[编辑]->[虚拟网络编辑器]对VMnet8进线[NAT 设置] c.调整[DHCP 设置]中的起始IP地址 ...

  10. Linux(4)系统管理

    系统管理 cal :查看当前月份和日历, 加-y查看整年日历 date :显示或设置时间 设置时间格式(需要管理员权限) date [MMDDhhmm[[CC]YY][.ss]]+format CC为 ...