Spring 之自动化装配 bean 尝试
【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 注解的作用
- 表明该类会作为组件类
- 告知 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 尝试的更多相关文章
- Spring实战之装配Bean
1.1Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当 ...
- 第2章—装配Bean—自动化装配Bean
自动化装配Bean 2.1.Spring配置可选方案 装配是依赖注入DI的本质,Spring提供了以下三种注入的装配机制: 在XMl中进行显式配置 在java中进行显式配置 隐式的Bean发现机制 ...
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- Spring基础(一)------装配Bean
一.Spring配置的可选方案 三种主要的装配机制: 在xml文件中进行显示配置: 在java中进行显示配置: 隐式的bean发现机制和自动装配. 使用建议:尽可能使用自动配置的机制,显示配置越少越好 ...
- Spring学习(二)--装配Bean
一.Spring装配机制 Spring提供了三种主要的装配机制: 1.在XML中进行显示配置 2.在Java中进行显示配置 3.隐式的bean发现机制和自动装配--自动化装配bean Spring可以 ...
- Spring总结 1.装配bean
本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...
- spring中自动装配bean
首先用@Component注解类: package soundsystem: import org.springframework.stereotype.Component; @Component p ...
- Spring学习系列(二) 自动化装配Bean
一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
随机推荐
- 那些年我读过的Blog(Ⅰ)
序 近期发现自己已经很久很久没有写过文章,其中包括公开的和非公开的Blog,于是自己去翻了以前很多关注的人的Blog,发现也已经有很大一部分没有更新了,遂有了本文,梳理一下自己那些年关注过并且现在已经 ...
- VUE:使用vue-cli脚手架无法安装npm install axios 的巨坑
使用命令 npm install axios 安装axios可能会报错,无法引用, 这个时候使用淘宝的镜像cnpm安装就可以了 cnpm install axios 如果没有安装cnpm,执行以下命令 ...
- 根本上解决npm install 报错“ajv-keywords@3.4.0 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.“
每次项目npm install 的时候都报这个错误, 然后网上找的方法就把这个 ajv重新安装下,感觉有点麻烦, 后来有次我把npm更新了一下(我的版本是: 6.1.0),更新到了最新版本,这个问题就 ...
- Sublime Text 格式化代码快捷键
首选项->按键绑定-用户 加入代码: {"keys": ["ctrl+alt+j"], "command": "reinde ...
- 怎样使用Intent传递对象
怎样使用Intent传递对象 我们能够使用Intent来启动Activity.开启服务Service,发送广播Broadcast,然后使用Intent传递主要的数据类型,如:布尔值,整型,字符串等 I ...
- sass学习之一:sass安装compass部署
主要参考 http://www.jianshu.com/p/5bfc9411f58f -------------------------------------------- sass基于ruby 需 ...
- cocos2d-X学习之主要类介绍:CCDirector
在cocos2d-x里面,游戏的任何时间,只有一个场景对象实例处于运行状态,该对象可以作为当前游戏内容的整体包对象 Cocos2d-x引擎除了提供了CCDirector,还提供了一个CCDisplay ...
- 微信公众号 openId 支付 php中file_get_contents与curl性能比较分析
w http://www.jb51.net/article/57238.htm
- 微信支付 php发送POST请求
https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=20_1 <_xml> <mch_id>132</mc ...
- python pip源配置,pip配置文件存放位置
https://blog.csdn.net/u013066730/article/details/54580789/ pip源配置文件可以放置的位置: Linux/Unix: /etc/pip.con ...