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并且如何将其装配在一起.当描 ...
随机推荐
- [Codeforces 452E] Three Strings
[题目链接] https://codeforces.com/contest/452/problem/E [算法] 构建后缀数组 用并查集合并答案即可 时间复杂度 : O(NlogN) [代码] #in ...
- 1068 Bash游戏 V3
1068 Bash游戏 V3 题目来源: Ural 1180 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一堆石子共有N个.A B两个人轮流拿 ...
- 数据结构与算法(2)----->字符串
1. 字符串的一些特点 1.1 广泛性 (1)字符串可以看作是字符类型的数组----->所以可能会涉及排序+查找; (2)很多问题都可以转化为字符串类型的方法去解决; 需要注意的是:用java ...
- MySQL5.6中新增特性、不推荐使用的功能以及废弃的功能
虽然已经使用MySQL5.6版本有一段时间了,但由于没有和之前的版本作过详细比较,所以对于哪些重要的或者不太重要的特性是在新版本中引入的,还有哪些特性已经或者将要从旧版本中移除的并没有一个十分全面的了 ...
- AtCoder Regular Contest 072 E:Alice in linear land
题目传送门:https://arc072.contest.atcoder.jp/tasks/arc072_c 题目翻译 给你一个数组\(D\),然后给你一个操作序列\(d\),每次操作可以将\(D\) ...
- C# 对象间的 深拷贝 实现
以下的这个类实现了 2个含有部分字段名字相同 的对象的 赋值拷贝. public class ShallowCopyHelper { public static voi ...
- JavaScript:Map使用
定义Map /** * Map * */ function Map() { /** 存放键的数组(遍历用到) */ this.keys = new Array(); /** 存放数据 */ this. ...
- git push -u origin master error: failed to push some refs to
1.问题描述 $ git push -u origin master To github.com:[github_name]/[github_repository_name].git ! [rejec ...
- C# 清除cookies
不同的浏览器会把cookie文件保存在不同的地方 以下是C# WebBrowser控件cookies的存放路径 C:\Users\{你的帐号名}\AppData\Local\Microsoft\Win ...
- Unity查找Editor下Project视图中特定的资源
[MenuItem("Tools/Check Text Count")] public static void CheckText () { //查找指定路径下指定类型的所有资源, ...