1.

 package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration //说明此类是配置文件
//@ComponentScan //开启扫描,会扫描当前类的包及其子包
//@ComponentScan(basePackages={"soundsystem", "video"})//扫描多个包
@ComponentScan(basePackageClasses={CDPlayer.class})//指定要扫描的类
public class CDPlayerConfig {
}

2.

 package soundsystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class CDPlayer implements MediaPlayer {
private CompactDisc cd; @Autowired
public CDPlayer(CompactDisc cd) { //依赖bean的id会叫"cd"???
this.cd = cd;
} public void play() {
cd.play();
} }

3.

 package soundsystem;
import org.springframework.stereotype.Component; @Component
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);
} }

4.

 package soundsystem;

 import static org.junit.Assert.*;

 import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
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) //会自动创建applicationcontext
@ContextConfiguration(classes=CDPlayerConfig.class)//指定配置文件
public class CDPlayerTest { @Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired
private MediaPlayer player; //默认id会是“player”??? @Autowired
private CompactDisc cd; @Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
} @Test
public void play() {
player.play();
assertEquals(
"Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles",
log.getLog());
} }
bean

SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在XML配置文件中引入JAVA配置文件 <import> 、<bean>

    一.在xml中引入xml,用<import> <?xml version="1.0" encoding="UTF-8"?> <be ...

  2. SPRING IN ACTION 第4版笔记-第二章-002-@ComponentScan、@Autowired的用法

    一.@ComponentScan 1. @Configuration //说明此类是配置文件 @ComponentScan //开启扫描,会扫描当前类的包及其子包 public class CDPla ...

  3. SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法

    1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...

  4. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  5. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-006-当构造函数有集合时的注入

    一.当构造函数有集合时,只能用<CONSTRUCTOR-ARG>,不能用C-NAMESPACE 二. 1. package soundsystem.collections; import ...

  6. SPRING IN ACTION 第4版笔记-第二章-004-Bean是否单例

    spring的bean默认是单例,加载容器是会被化,spring会拦截其他再次请求bean的操作,返回spring已经创建好的bean. It appears that the CompactDisc ...

  7. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-007-以set方法注入<property>\p-namespace\util-space

    一.注入简单属性 package soundsystem.properties; import org.springframework.beans.factory.annotation.Autowir ...

  8. SPRING IN ACTION 第4版笔记-第二章Wiring Beans-005-<constructor-arg>和c-namespace

    1. package soundsystem; public class SgtPeppers implements CompactDisc { private String title = &quo ...

  9. Spring In Action 第4版笔记-第一章-001架构

    1.Spring’s fundamental mission: Spring simplifies Java development. 2.To back up its attack on Java ...

随机推荐

  1. 简单的实现QQ通信功能(二)

    第二部分:功能需求以及大体思路 一:功能需求: 1. 角色:登录用户. 2. 登录: (1)检查用户名和密码是否正确,正确登录成功,否则提醒用户名或密码错误. (2)登录时可以选择登录状态,送入数据库 ...

  2. MongoDB 2.6.x 的安装部署

    1. 下载mongodb 2.6.x版本的zip包,在D盘创建目录MongoDB,解压缩到D:\MongoDB目录. 创建数据库目录D:\MongoDB\data,接下来打开命令行窗口,切换到D:\M ...

  3. poj1308 Is It A Tree?(并查集)详解

    poj1308   http://poj.org/problem?id=1308 题目大意:输入若干组测试数据,输入 (-1 -1) 时输入结束.每组测试数据以输入(0 0)为结束标志.然后根据所给的 ...

  4. windows下Nginx配置与测试

    1.获取 下载地址:http://nginx.org/en/download.html        参考网址:http://nginx.org/en/docs/windows.html 2.准备工作 ...

  5. C#获取当前路径,获取当前路径的上一层路径

    C#获取当前路径的方法如下: (1)string path1 = System.Environment.CurrentDirectory; //C:\...\bin\Debug -获取和设置当前工作目 ...

  6. 基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置

    第一章. 背景简介 对于大多数企业应用,都希望能做到7*24小时不间断运行.要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群.系统变为集群 ...

  7. JavaScript之Function类型

    1. 创建方式 //1.函数声明 function sum(num1,num2){ return num1+num2; } //2.函数表达式 var sum = function(num1,num2 ...

  8. sql查询结果集根据指定条件排序的方法

    oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 有几种办法改变这种情况: (1)用 nvl 函数或decode 函数 将null ...

  9. UVA 10739 String to Palindrome(动态规划 回文)

    String to Palindrome 题目大意:给出一个字符串s,现在可以进行3种操作(添加字母,删除字母,替换字母),将其变成回文串,求出最少的操作次数.比如abccda,可以用删除操作,删除b ...

  10. height为100%的问题

    问题描述 很多同学,对于设置div 的高度为100%时,有疑惑. 设置div 的高度为100%,意思是此 div 的高度 铺满父元素. 那么 怎么使 div 铺满浏览器屏幕? 代码 <!DOCT ...