Spring入门(三)
测试Spring Bean的自动化装配
方式一:使用配置文件方式启动组件扫描
准备一个接口
package soundsystem;
public interface CompactDisc {
void play();
}
实现准备的接口,使用@Component注解(让Spring容器发现,以创建该类实例)
package soundsystem;
import org.springframework.stereotype.Component; @Component // 该注解作用:让该类作为组件类,并告知spring为这个类创建bean
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);
}
}
在上述实现类下创建一个java文件,用@ComponentScan注解标注(默认扫描该java文件所在的包及其子包),并且用Configuration注解()
package soundsystem; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
public class CDPlayerConfig {
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="soundsystem" /> </beans>
测试:用SpringTest,加入jar包,在类文件中注入接口实现类,用@Autowired注解(将该注解指定的bean注入进该实例中使用),类文件上使用@RunWith注解,@ConfigurationContext注解
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(locations="classpath:applicationContext.xml")
public class CDPlayerTest {
@Autowired
private CompactDisc cd; @Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
cd.play();
}
}
方式二:用注解方式启动组件扫描
创建接口与创建接口的实现类方法相同,此处不再赘述
java文件
package soundsystem; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan // 启动组件扫描,扫描与配置类相同的包
public class CDPlayerConfig {
}
测试文件
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)//需要在CDPlayerConfig中加載配置
public class CDPlayerTest { @Autowired
private CompactDisc cd; @Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
cd.play();
}
}
@RunWith:参考https://www.jianshu.com/p/70aee958e980
同样在自学Spring的小伙伴,如果看到我的博客,希望留下你的联系方式,我们一起加油!!!
Spring入门(三)的更多相关文章
- Spring入门(三):通过JavaConfig装配bean
上一篇博客中,我们讲解了使用组件扫描和自动装配实现自动化装配bean,这也是最好的使用方式. 但是某些场景下,我们可能无法使用自动装配的功能,此时就不得不显式的配置bean. 比如我们引用了一个第三方 ...
- spring入门(三) 使用spring mvc
1.建立project / module 新建空的project:springMvcStudy 新建module:type maven-webapp,名字mvcStudy 2.为module设置Sou ...
- Spring入门详细教程(三)
前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...
- 【Spring Framework】Spring入门教程(三)使用注解配置
本文主要介绍四个方面: (1) 注解版本IOC和DI (2) Spring纯注解 (3) Spring测试 (4) SpringJDBC - Spring对数据库的操作 使用注解配置Spring入门 ...
- Spring入门(10)-Spring JDBC
Spring入门(10)-Spring JDBC 0. 目录 JdbcTemplate介绍 JdbcTemplate常见方法 代码示例 参考资料 1. JdbcTemplate介绍 JdbcTempl ...
- Spring入门(9)-AOP初探
Spring入门(9)-AOP初探 0. 目录 什么是面向切面编程 AOP常见术语 AOP实例 参考资料 1. 什么是面向切面编程 Aspect Oriented Programming(AOP),即 ...
- spring 入门篇
spring 入门篇 相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...
- Spring第三天
Spring第三天 整体课程安排(3天+2天): 第一天:Spring框架入门.IoC控制反转的配置管理.Spring Web集成.Spring Junit集成. 第二天:Spring AOP面向切面 ...
- spring入门详细教程(五)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
- Spring入门详细教程(四)
前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...
随机推荐
- centos7 安装python虚拟环境
本篇主要介绍centos7系统下,安装python3虚拟环境.环境:系统centos7,源代码安装python3,/usr/bin/python3为自己安装的. 安装支持包 yum install p ...
- 一、最新Kafka单节点部署+测试 完整
每次学一个东西从基础的开始,循序渐进. 不急不躁,路还很长. 所有教程都是学习汪文君大神的kafka教程的. 一.部署 这里选的kafka版本是 0.10.2.1 下载连接 https://dow ...
- eslint 禁用命令
/* eslint-disable */ ESLint 在校验的时候就会跳过后面的代码 还可以在注释后加入详细规则,这样就能避开指定的校验规则了 /* eslint-disable no-new */ ...
- vue对象侦测
http://blog.csdn.net/yihanzhi/article/details/74200618 数组:this.$set(this.arr,index,value)
- 我的scoi2018
高一,很尴尬,凉~ -------- 大家好,我是分界线,我弱弱的说本次采用倒序的写作手法 -------- 故事是这样讲的: Day0: 刚刚去那个电科搞的集训,早上才考了一波模拟赛,下午就过来住酒 ...
- LeetCode Array Easy169. Majority Element
Description Given an array of size n, find the majority element. The majority element is the element ...
- caffer的三种文件类别
solver文件 是一堆超参数,比如迭代次数,是否用GPU,多少次迭代暂存一次训练所得参数,动量项,权重衰减(即正则化参数),基本的learning rate,多少次迭代打印一次loss,以及网络结构 ...
- linux netstat 统计连接数查看外部(转)
转自:http://boy-liguang.blog.sohu.com/187052443.html linux netstat 统计连接数查看外部 2011-10-11 08:52阅读(16333) ...
- Linux新建环境变量快速切换到文件夹(export)
如果有一个文件夹目录很深/home/user/aaa/bbb/ccc/ddd/eee/fff/ggg,但是经常要跳转到这个文件夹.一个简单的办法就是给这个文件夹建立一个类似$PATH那样的环境变量,如 ...
- ac自动机fail树上按询问建立上跳指针——cf963D
解法看着吓人,其实就是为了优化ac自动机上暴力跳fail指针.. 另外这题对于复杂度的分析很有学习价值 /* 给定一个母串s,再给定n个询问(k,m) 对于每个询问,求出长度最小的t,使t是s的子串, ...