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;

@Componentpublic 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;

@Componentpublic 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
@ComponentScanpublic 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作为实例化类。





      本文转自zsdnr  51CTO博客,原文链接:http://blog.51cto.com/12942149/1932759,如需转载请自行联系原作者





相关文章
|
8天前
|
Java uml Spring
手写spring第四章-完善bean实例化,自动填充成员属性
手写spring第四章-完善bean实例化,自动填充成员属性
17 0
|
25天前
|
Java 应用服务中间件 Spring
Spring系列文章:Bean的作⽤域
Spring系列文章:Bean的作⽤域
|
25天前
|
Java Spring 容器
Spring系列文章:Bean的获取⽅式
Spring系列文章:Bean的获取⽅式
|
1月前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
34 1
|
2月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
83 0
|
4天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
14 1
|
7天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
21 2
|
8天前
|
XML Java 数据格式
手写spring第七章-完成便捷实现bean对象初始化和销毁方法
手写spring第七章-完成便捷实现bean对象初始化和销毁方法
6 0
|
8天前
|
XML Java 数据格式
手写spring第六章-实现应用上下文,完成bean的扩展机制
手写spring第六章-实现应用上下文,完成bean的扩展机制
14 0
|
8天前
|
设计模式 搜索推荐 Java
手写spring第三章-重构,使用依赖关系完善实例化bean操作
手写spring第三章-重构,使用依赖关系完善实例化bean操作
15 0