【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. poj 3204(最小割)

    题目链接:http://poj.org/problem?id=3204 思路:显然只有增大那最小割边集上的边才能增加最大流,因此,我们可以先跑一遍最大流,然后对于那些满足条件的边u->v,当且仅 ...

  2. TypeScript 接口(三)

    TypeScript的核心原则之一是对值所具有的结构进行类型检查. 接口初始: interface objProperty { name: string } function printName(na ...

  3. PowerDesigner使用教程3

    from:http://www.cnblogs.com/langtianya/archive/2013/03/08/2949118.html PowerDesigner是一款功能非常强大的建模工具软件 ...

  4. 自定义ScrollView 支持添加头部

    自定义ScrollView 支持添加头部并且对头部ImageView支持放大缩小,上滑头部缩小,下滑头部显示放大 使用方式: scrollView = (MyScrollView) findViewB ...

  5. 认识tornado(四)

    接下来我们看一下helloword.py的唯一一个handler. 1 class MainHandler(tornado.web.RequestHandler): 2 def get(self): ...

  6. c++ 流继承关系

  7. MVC Model验证疑难杂症

    Q1:有验证但是还是能进入控制器(Controller) 排查之后发现js报错:VM109:1 Uncaught SyntaxError: Unexpected token u in JSON at ...

  8. 【转】Startssl SSL 证书申请图解

    一.什么是 SSL 证书,什么是 HTTPS 网站? SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secu ...

  9. Android 中 js 和 原生交互

    Android中的WebView 中加载的URL 默认是在手机浏览器中加载的,我们可以覆盖这种默认的动作,让网页在WebView中打开.通过设置WebView的WebViewClent 达到这个效果. ...

  10. 编译型 解释型 C++工作原理

    C++教程_w3cschool https://www.w3cschool.cn/cpp/ C++工作原理: C++语言的程序因为要体现高性能,所以都是编译型的.但其开发环境,为了方便测试,将调试环境 ...