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. error: Error: No resource found that matches the given name (at 'layout_above' with value '@id/btnLayout').

    今天在练习fragment碎片的时候,进行界面布局的时候出现了这个问题. 后来解决后发现原因很简单:就是因为在布局xml文件中,引用ID和声明ID的顺序必须保证声明在前,引用在后.和布局的顺序无关. ...

  2. 闲话:你今天OO了吗?

    如果你的分析习惯是在调研技术的时候最先弄清楚有多少业务流程,先画出业务流程图,然后顺藤摸瓜,找出业务流程中每一步骤的参与部门或岗位,弄清楚在这一步参与者所做的事情和填写表单的结果,并关心用户是如何把这 ...

  3. YII中引用自定义类

    如果通过actions方法引用其他自定义类时 <?php class LoginController extends Controller { public function actionInd ...

  4. MVC框架是什么

     MVC (Modal View Controler)本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使 ...

  5. zzzzw_在线考试系统③完结篇

    昨天填完原本打算写有关“学生考试部门”的总结,但是因为时间来不及,所以推迟到今天来写. 至于最后的:“老师登录”部门就没什么好说的了,只要会了“管理员部分”和“学生考试部分”的书写,剩下就只是耐心的一 ...

  6. Android应用程序消息处理机制笔记

    看老罗的Android源码情景分析学习的时候,边抄边理解再总结.希望能为面试提供点帮助吧. 1.Android应用程序是通过消息来驱动,Android应用程序每一个线程在启动时,都可以首先在内部创建一 ...

  7. Solr4.8.1与Tomcat7整合

    Solr4.8.1和Tomcat7都可以到官方网站去下载,我这里就不多说了,如下图. 这里我们首先解压Solr-4.8.1.zip,再解压Tomcat,解压后,再在当前文件夹下建2个文件夹,一个用来放 ...

  8. json(gson) 转换html标签带来的麻烦

    gson 转换html标题时,会把html(特殊字符转换为unicode编码) ,所以为了避免这个问题GsonBuilder类 有一个 disablehtmlEscaping方法. 就可以让gson类 ...

  9. iOS中RGB颜色转换

    iOS中RGB常用的色值,同时可将对颜色的设置定义成宏,方便开发应用,如: // RGB颜色转换(16进制->10进制) #define UIColorFromRGB(rgbValue) [UI ...

  10. 安装"MySQLdb"一波三折.

    在慕课网学习课程"Python操作MySQL数据库",安装"MySQLdb"时遇到问题. 先是找错地方: 百度搜索"Mysql for Python& ...