Spring自动化装配bean
1. 场景
用CD(Compact disc)和CD播放器(CDPlayer)阐述DI(依赖注入)。
如果不将CD插入(注入)到CDPlayer中,那么CDPlayer其实没有太大的用处,所以,可以这样说:CDPlayer依赖于CD才能完成它的使命。
2. 架构图示

3. 代码
接口: CompactDisc.java
package soundsystem;
public interface CompactDisc {
void play();
}
接口: MediaPlayer.java
package soundsystem;
public interface MediaPlayer {
void play();
}
SgtPeppers.java
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"; @Override
public void play() {
System.out.println("Playing " + title + " by " + artist);
} }
注:SgtPeppers类上使用了@Component注解。这个简单的注解表明该类会作为组件类,并告知Spring要为这个类创建bean
CDPlayer.java
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) {
this.cd = cd;
} @Override
public void play() {
cd.play();
} }
不过,组件扫描默认是不启用的,我们还需显式配置一下Spring,从而命令它去寻找带有@Component注解的类,并未其创建bean。下例中使用了@ComponentScan注解,这个注解能够在Spring中启用组件扫描。如没有其他配置,@ComponentScan默认会扫描与配置类相同的包:soundsystem
CDPlayerConfig.java
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan
public class CDPlayerConfig {
}
测试CDPlayerTest.java
package soundsystem; import static org.junit.Assert.*;
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)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest { @Autowired
private MediaPlayer player; @Autowired
private CompactDisc cd; @Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
} @Test
public void play() {
player.play();
} }
自动装配就是让Spring自动满足bean依赖的一种方法,在满足依赖的过程中,会在Spring应用的上下文中寻找匹配某个bean需求的其他bean。为了声明要进行自动装配,我们借助Spring的@Autowired注解。
上述代码中,在构造器中添加了@Autowised注解,这表明当Spring创建CDPlayer bean的时候,会通过这个构造器来进行实例化,并传入一个可设置为CompactDisc类的bean,在上面的代码中,SgtPeppers被声明为组件,并实现了CompactDisc接口。因此,在实际运行中会把SgtPeppers作为实例化类。
Spring自动化装配bean的更多相关文章
- Spring 之自动化装配 bean 尝试
[Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...
- 第2章—装配Bean—自动化装配Bean
自动化装配Bean 2.1.Spring配置可选方案 装配是依赖注入DI的本质,Spring提供了以下三种注入的装配机制: 在XMl中进行显式配置 在java中进行显式配置 隐式的Bean发现机制 ...
- 二、spring中装配bean
在spring框架中提供了三种 bean的装配方式,当然这三种装配方式是可以灵活的进行组合使用的,项目中使用最多的是自动装配bean的方式,也就是通过注解的方式进行bean的装配,一下是四种装配方式的 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring学习系列(二) 自动化装配Bean
一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
- Spring容器装配Bean的三种方式
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- 自动化装配Bean
一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...
随机推荐
- 循环神经网络RNN公式推导走读
0语言模型-N-Gram 语言模型就是给定句子前面部分,预测后面缺失部分 eg.我昨天上学迟到了,老师批评了____. N-Gram模型: ,对一句话切词 我 昨天 上学 迟到 了 ,老师 批评 了 ...
- kNN算法个人理解
新手,有问题的地方请大家指教 训练集的数据有属性和标签 同类即同标签的数据在属性值方面一定具有某种相似的地方,用距离来描述这种相似的程度 k=1或则较小值的话,分类对于特殊数据或者是噪点就会异常敏感, ...
- Extjs6(特别篇)——项目自带例子main.js拆分详解
本文基于extjs6.0.0 一.拆分代码来看看 1.主页面main是个tab页: 写一些页面的依赖: 标明页面的controller和viewModel Ext.define('Learning.v ...
- js函数中this的指向
本文是我个人对this指向的一些理解,如有不足之处,还望大家可以批评指正,在此先谢过了! 首先,我们来回顾一下ES5里函数的几种调用方式: 1⃣️直接调用 foo(); 2⃣️方法调用 obj.foo ...
- 玩转Eclipse--如何使用eclipse可以更好的提高我们的工作效率
工欲善其事必先利其器,更加了解我们的开发工具有利于提高开发效率,而合理使用快捷键可以使我们事半功倍,这里收集了eclipse中的几种常见设置,eclipse的优化以及非常全面的快捷键介绍,大家有用到的 ...
- java并发程序——BlockingQueue
概述 BlockingQueue顾名思义'阻塞的队列',是指在:队列的读取行为被阻塞直到队列不为空时,队列的写入行为被阻塞直到队列不满时.BlockingQueue是java.util.concurr ...
- 观察者模式(Observer)发布、订阅模式
观察者模式定义了对象之间一对多的依赖关系,这样一来,当一个对象改变时,他的所有依赖者都会收到通知并自动更新. 模式中的角色 1.抽象主题(Subject):它把所有观察者对象的引用保存到一个聚集里 ...
- postgres导入其他数据库数据
最近对postgres数据库进行深入研究,将原来项目中使用的sqlserver数据库中的数据表导入postgres,网上搜索postgres数据导入,除空间数据库可以通过PostGIS 2.0 Sha ...
- Brief introduction to Java String Split 【简单介绍下Java String Split】
Split is a common function in Java. It split a full string to an array based on delimeter. For examp ...
- python3 time模块与datetime模块
time模块 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平 ...