项目包结构:

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. PHPOffice/PHPExcel生成省市区三级联动的excel表格

    最近公司需要用到一个省市区三级联动的excel表格,但是数据都在数据库,又太多,人工不好制作,就让我这个phper来帮忙啦. 主要用到的是excel的定义名称,数据验证.其中数据验证的列表只能是一列或 ...

  2. C++学习(五)入门篇——基本类型

    面向对象编程的本质是设计并扩展自己的数据类型,让类型和数据匹配. 内置C++分成两种类型:基本类型和复合类型 1.简单变量 程序需要存储信息时,必须记录三个基本属性 (1)信息将存储在哪 (2)要存储 ...

  3. RxSwift 系列(五) -- Filtering and Conditional Operators

    前言 本篇文章将要学习RxSwift中过滤和条件操作符,在RxSwift中包括了: filter distinctUntilChanged elementAt single take takeLast ...

  4. Spring AOP 和 动态代理技术

    AOP 是什么东西 首先来说 AOP 并不是 Spring 框架的核心技术之一,AOP 全称 Aspect Orient Programming,即面向切面的编程.其要解决的问题就是在不改变源代码的情 ...

  5. Spring+SpringMVC+MyBatis集成学习笔记【一】

    一,首先要清楚,SpringMVC其实就是Spring的一个组件       例如我们知道Spring中有类似于,AOP TX等等类似的组件,所以SpringMVC其实就是Spring的一个组件,是S ...

  6. Spring源码情操陶冶-ContextLoaderListener

    前言:通过实例结合源码的方式解读,其中涉及到的文件来自于博主的Github毕设项目wxServer Note: Springboot应用不在本文章讨论范围 web.xml中启用Spring 在一般的w ...

  7. Hibernate的系统 学习

    Hibernate的系统 学习 一.Hibernate的介绍 1.什么是Hibernate? 首先,hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多比如:iBATIS,myBat ...

  8. 关于Visual Studio调试 无效指针提示

    前几天遇到了这个问题,编译没问题,直接运行没问题 但是一调试,会提示无效指针,(按Ctrl+F5可以运行,但按F5提示无效指针) 只要这样,新建C:\ProgramData\Microsoft Vis ...

  9. NYOJ--284--广搜+优先队列--坦克大战

    /* Name: NYOJ--284--坦克大战 Author: shen_渊 Date: 14/04/17 19:08 Description: 广度优先搜索+优先队列 注意清空地图 对输入地图进行 ...

  10. ubuntu下统计目录及其子目录文件个数

    查看某目录下文件的个数 ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l 查看某目录下文件的个数,包括子目录里的. l ...