【Spring之自动化装配bean尝试】

1、添加dependencies如下所示(不是每一个都用得到

  <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

2、创建可以被发现的“ bean ”

首先定义CD的概念

package soundsystem;

public interface CompactDisc {
void play();
}

创建带有 @Component 注解的实现类

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

@Component 注解的作用

  1. 表明该类会作为组件类
  2. 告知 Spring 要为这个类创建 bean

3、声明组件扫描,下面这个类与 CD 实现类在同一个包下。

package soundsystem;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan
public class CDplayerConfig { }

注解 1 @Configuration 告知 Spring的应用上下文 要在该类中加载配置

注解 2 @ComponentScan 告知 Spring的应用上下文  要启用组件扫描,默认扫描与配置类相同的包

4、测试自动化装配 bean

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);
}
}

- @RunWith 创建 Spring的应用上下文

- @ContextConfiguration 加载配置 --> 启用组件扫描 --> 扫描组件,发现带 @Component 注解的 CD实现类 --> 自动创建 bean

- @Autowired 将 CD实现类 对象 注入到测试代码中

5、自动装配 bean 的数量是不限的

public class CDPlayerTest {

    @Autowired
private CompactDisc cd; @Autowired
private CompactDisc cd2; @Autowired
private CompactDisc cd3; @Autowired
......... @Autowired
private CompactDisc cdn; @Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
assertNotNull(cd2);
}
}

上述代码有误,无限指的是 只需要一个

@ComponentScan

注解,就可自动创建无限的bean,这些bean对应于各自不同的类

扫描产生的bean默认为单例

【Spring 自动化装配 bean 相关注解 深入探讨】

自动化装配总结起来就是:

创建可用的 bean --> 声明组件扫描 --> 启用配置,声明自动装配

这中间还有一些可做的(可自定义)的事情,

例如说,为组件扫描的 bean 命名、设置组件扫描的基础包(定义扫描范围),

为各种东西(构造器、方法)添加 @Autowired 注解(该注解有个required属性,

自动装配的缺陷在于有多个匹配的 bean 时将产生歧义(来自《Spring in Action》。。。

Spring 之自动化装配 bean 尝试的更多相关文章

  1. Spring实战之装配Bean

    1.1Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当 ...

  2. 第2章—装配Bean—自动化装配Bean

    自动化装配Bean 2.1.Spring配置可选方案 ​ 装配是依赖注入DI的本质,Spring提供了以下三种注入的装配机制: 在XMl中进行显式配置 在java中进行显式配置 隐式的Bean发现机制 ...

  3. spring的自动装配Bean与自动检测Bean

    spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...

  4. Spring基础(一)------装配Bean

    一.Spring配置的可选方案 三种主要的装配机制: 在xml文件中进行显示配置: 在java中进行显示配置: 隐式的bean发现机制和自动装配. 使用建议:尽可能使用自动配置的机制,显示配置越少越好 ...

  5. Spring学习(二)--装配Bean

    一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...

  6. Spring总结 1.装配bean

    本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...

  7. spring中自动装配bean

    首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...

  8. Spring学习系列(二) 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

  9. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

随机推荐

  1. 深入学习QWidget-1

    1.QWidget对事件的抓取和放手.主要有例如以下几个接口 grabGesture 抓取输入手势 ungrabGesture 取消抓取手势 grabKeyboard 抓取键盘输入 grabMouse ...

  2. js的实例方法和静态方法分析

    var Person=function(){}; Person.say=function(){ console.log('I am a Person,I can say.') }; Person.pr ...

  3. 2-SAT求任意解模板

    int stk[N],vis[N],low[N],link[N],mark[N]; int top,index,id,du[N];//记录入度数 int pre[N],cnt,g[N];// g 用来 ...

  4. hiho一下第107周《Give My Text Back》

    题目链接:传送门 题目大意:给你多组格式不标准的字符串句子,要你恢复到标准格式(单词中间只有一个空格或者一个逗号一个空格,句子的首字母大写其他小写,遇到回车也要换行) 题目思路:直接按题意模拟,注意几 ...

  5. 2 CDuiString的bug

    重温了一下 Effective C++,发现这就是条款24所指出的问题,看来读书百遍不如写代码一遍啊 在Notify处理消息时会有很多if语句,我通常喜欢把常量放在双等号前面,变量放在后面,比如:   ...

  6. Vulnerabilities by Type

    w http://hackergossips.com/cross-site-scriptingxss-and-preventing/

  7. Json反序列化Map的key不能是Object

    使用json作为数据传输格式,碰到一个问题.我希望传输的是一个Map<Target, TargetInfo>其中Target是一个对象,作为map的一个key public class T ...

  8. Python--(并发编程之线程Part2)

    GIL只能保证垃圾回收机制的安全,进程中的数据安全还是需要自定义锁 线程执行代码首先要抢到GIL全局锁,假设线程X首先抢到,以为要抢到自定义锁要执行代码,所以这个线程在执行代码的时候就很容抢到了自定义 ...

  9. mysql 中sum (if()) 用法

    原表: id    fenlei     time 1      分类1      20130316 2      分类2      20130316 3      分类3      20130317 ...

  10. HTML(form标签)、CSS

    一.表单标签<form> 功能:表单用于向服务器传输数据,从而实现用户与Web服务器的交互. 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含t ...