《Spring in Action》第二章--自动化装配bean+音响系统例子的实现

实验环境:
1、eclipse开发工具
https://www.eclipse.org/downloads/
2、Java JDK(IDE有自己的编译环境,所以只需安装JRE即可)
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
3、Spring框架 spring-framework-4.0.0.RELEASEdist.zip
http://repo.spring.io/release/org/springframework/spring/
4、commons-logging下载 commons-logging-1.2-bin.zip
http://commons.apache.org/proper/commonslogging/download_logging.cgi
5、JUnit4框架(使用JUnit4.12) junit-4.12.jar
http://maven.outofmemory.cn/junit/junit/4.1/
6、hamcrest框架(JUnit4.12中不包含hamcrest框架所以需要单独下载)
hamcrest-all-1.3.jar
http://repo2.maven.org/maven2/org/hamcrest/hamcrest-all/1.3/

在Spring中,对象无需自己查找或创建与其所关联的其他对象,容器负责把需要相互协作的对象引用赋予各个对象。
装配(wiring):创建应用对象之间协作关系的行为。

配置Spring容器的三种机制如下:

  • 在XML中进行显示配置
  • 在Java中进行显示配置
  • 隐式的bean发现机制和自动装配

Spring从两个角度实现自动化装配:
1、组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean
2、自动装配(autowiring):Spring自动满足bean之间的依赖

音响系统的例子

一、Spring环境搭建

1、新建Java Project

File–>New–>Project–>Java Project

这里写图片描述

2、导入相关的包

右键项目名称SoundSystem–>Bulid Path–>Configure Bulid Path

这里写图片描述

这里写图片描述

将spring-framework-4.0.0.RELEASEdist.zip解压到本地文件夹,点击“Add External JARs”后在弹出的对话框中选择spring-framework-4.0.4.RELEASE–>libs,简单起见将libs文件夹中的.jar文件全部选择加入。
再依次加入commons-logging-1.2.jar、junit-4.12.jar和hamcrest-all-1.3.jar。

这里写图片描述

这里写图片描述

二、在Java中建立CD,定义CompactDisc接口

右键src–>New–>Package

这里写图片描述

右键soundsystem–>New–>Interface

这里写图片描述

CompactDisc.java代码如下:

package soundsystem;

public interface CompactDisc {
    void play();
}

三、类SgtPeppers实现CompactDisc接口

@Component注解表明该类会作为组件类,并告知Spring要为这个类创建bean。

SgtPeppers代码如下:

package soundsystem;
import org.springframework.stereotype.Component;

@Component      //表示该类作为组件类
public class SgtPeppers implements CompactDisc{

   private String title = "Stg.Pepper's Lonely Hearts Club Band";
   private String artist = "The Beatles";

   public void play() {
        System.out.println("Playing " + title + " by " + artist);
    }
}

四、启动组件扫描

组件扫描默认是不启动的,需要显示配置Spring,命令它去寻找带有@Component注解的类并为其创建bean。

CDPlayerConfig.java代码如下:

package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan   
public class CDPlayerConfig {
}

类CDPlayerConfig通过Java代码定义了Spring装配规则,类中并没有显示声明任何bean,通过使用@ComponentScan注解启用组件扫描。如果没有其他配置的话,默认扫描与配置类相同的包即本例中的soundsystem。

若要扫描其他的包,在@ComponentScan的value属性中指明包的名称:
@Configuration
@ComponentScan(“soundsystem”)
若要表明设置的是基础包,可通过basePackages属性配置:
@Configuration
@ComponentScan(basePackages=”soundsystem”)
若要设置多个基础包:
@Configuration
@ComponentScan(basePackages={“soundsystem”,”video”})
也可以将其指定为包中所包含的类或接口,这些类所在的包会作为组件扫描的基础包:
@Configuration
@ComponentScan(basePackageClasses={CDPlayer.class,DVDPlayer.class})

五、测试组件扫描

创建简单的JUnit测试,它会创建Spring上下文,并判断CompactDisc是否有创建。

CDPlayerTest.java代码如下:

package soundsystem;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= {CDPlayerConfig.class})
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull() {
        assertNotNull(cd);
    }
}

这里写图片描述

运行程序结果如下:以测试成功的颜色显示

这里写图片描述

第一个使用Spring框架的例子终于运行成功啦(^o^)/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值