spring中自动装配bean
首先用@Component注解类:
package soundsystem;
import org.springframework.stereotype.Component;
@Component
public class TestBean{
……
}
@Component("bean id")可以为Bean命名相当于XML中的<bean name = "bean id",class="soundsystem.TestBean"></bean>
开启组件扫描spring才能自动装配bean,创建一个@ComponentScan注解的类
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
//本来是import org.springframework.context.annotation.Con; 但是导入的包里没有Con只能先用*代替,反正能用 import org.springframework.context.annotation.Con;
@Configuration//不添加上包无法使用
@ComponentScan
public class TestBeanScan{ }
开启默认扫描,spring将扫描由@Component注解的类(在TestBeanScan类的soundsystem包或者其子包下),为其创建一个bean。
建一个JUnit测试类
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);
}
}
运行时报错

网上查,缺少两个包:hamcrest-core-1.3.rc2.jar,hamcrest-library-1.3.rc2.jar;(JUnit依赖hamcrest框架)
同时JUnit包的版本也得是JUnit4.12,我之前的是JUnit4.7,也会报错。
Build path,重新测试

如书上所说测试运行器确实出现绿色,但是控制台的输出似乎没有达到预期,不知道是什么原因,如果您知道还请在评论区告知一声,谢谢了。
自制了一个思维导图,总结学习过程

spring中自动装配bean的更多相关文章
- Spring中自动装配(转)
Spring中有四种自动装配类型,分别为:byName,byType,constructor,autodetect,下面来分别介绍一下这些是如何自动装配的 <bean id="foo& ...
- Spring的自动装配Bean
spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>.<constructor-arg>).IOC容器会 ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- Spring中自动装配的模式
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484037.html 自动装配模式 Reference https://docs.spring.io ...
- Spring总结 1.装配bean
本随笔内容要点如下: 依赖注入 Spring装配bean的方式 条件化装配 一.依赖注入 我理解的依赖注入是这样的:所谓的依赖,就是对象所依赖的其他对象.Spring提供了一个bean容器,它负责创建 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...
- spring学习总结——装配Bean学习一(自动装配)
一.Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当描 ...
随机推荐
- 《c# 实现p2p文件分享与传输系统》 二、 设计
c#实现P2P文件分享与传输系统 二.设计 在上一篇文章中,介绍了P2P网络的常用模型,并确定了EasyP2P系统的框架,本文将就此设计完成它的主要结构和运作流程. 1. 首先是Tracker Ser ...
- PowerShell 总结
PowerShell 总结 1. 2. 3. 参考学习资料 (1). PowerShell 在线教程 (2). 利用Powershell在IIS上自动化部署网站 视频教程: (3). Windows ...
- ScrollView cannot scroll in Slidinguppanellayout 解决办法
xml源码如下 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:an ...
- 计算机网络HTTP、TCP/IP包
参考: TCP-IP数据包结构详解 HTTP报文格式详解 Http协议报文格式 HTTP请求/响应报文结构 [Java知识]GET和POST请求的区别
- F - 棋盘问题
F - 棋盘问题 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- linux正则表达式基础
linux中awk,sed,grep等 命令使用区别正则表达式基础 在最简单的情况下,一个正则表达式看上去就是一个普通的查找串.例如,正则表达式"testing"中没有包含任何元字 ...
- Poll: Most Americans&n…
Most Americans support tough new measures to counter gun violence, including banning assault weapons ...
- 2.5玩转xargs
我们可以利用管道将一个命令的stdout(标准输出)重定向到另一个命令的stdin(标准输入).有些命令只能以命令行参数的形式接受数据,而无法通过stdin接受数据流.这时候就没法使用管道.那么xar ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
- Windows Error Codes
http://www.briandunning.com/error-codes/?source=Windows Windows Error Codes List All Error Codes | S ...