测试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入门(三)的更多相关文章

  1. Spring入门(三):通过JavaConfig装配bean

    上一篇博客中,我们讲解了使用组件扫描和自动装配实现自动化装配bean,这也是最好的使用方式. 但是某些场景下,我们可能无法使用自动装配的功能,此时就不得不显式的配置bean. 比如我们引用了一个第三方 ...

  2. spring入门(三) 使用spring mvc

    1.建立project / module 新建空的project:springMvcStudy 新建module:type maven-webapp,名字mvcStudy 2.为module设置Sou ...

  3. Spring入门详细教程(三)

    前言 本篇紧接着spring入门详细教程(二),建议阅读本篇前,先阅读第一篇和第二篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/101 ...

  4. 【Spring Framework】Spring入门教程(三)使用注解配置

    本文主要介绍四个方面: (1) 注解版本IOC和DI (2) Spring纯注解 (3) Spring测试 (4) SpringJDBC - Spring对数据库的操作 使用注解配置Spring入门 ...

  5. Spring入门(10)-Spring JDBC

    Spring入门(10)-Spring JDBC 0. 目录 JdbcTemplate介绍 JdbcTemplate常见方法 代码示例 参考资料 1. JdbcTemplate介绍 JdbcTempl ...

  6. Spring入门(9)-AOP初探

    Spring入门(9)-AOP初探 0. 目录 什么是面向切面编程 AOP常见术语 AOP实例 参考资料 1. 什么是面向切面编程 Aspect Oriented Programming(AOP),即 ...

  7. spring 入门篇

    spring 入门篇         相对于Hibernate(冬眠),Spring(春天),具有更多的诗意与希望的感觉,是为了解决传统J2EE开发效率过低.开发商之间不统一.没有真正实现“写一次到处 ...

  8. Spring第三天

    Spring第三天 整体课程安排(3天+2天): 第一天:Spring框架入门.IoC控制反转的配置管理.Spring Web集成.Spring Junit集成. 第二天:Spring AOP面向切面 ...

  9. spring入门详细教程(五)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

  10. Spring入门详细教程(四)

    前言 本篇紧接着spring入门详细教程(三),建议阅读本篇前,先阅读第一篇,第二篇以及第三篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/ ...

随机推荐

  1. layout(布局)组件

    一.依赖于 Panel(面 板)组件和 resizable(调整大小)组件. 二.class加载方式    <div id="box" class="easyui- ...

  2. EntityFramework 知识点与sql优化汇总

    一.EntityFramework modelBuilder.Entity<Domain.UseOilPlanDetail>().HasRequired(x => x.MainOil ...

  3. 【记录】安装mysql Install/Remove of the Service Denied!错误的解决办法

    最近安装 Mysql 5.6版本,在安装的过程中出现Install/Remove of the Service Denied!错误!(本人Win10 系统) 出现此问题是由于当前用户权限不够,需要以管 ...

  4. KiCAD输出生产文件

    KiCAD输出生产文件 本文包括PCB生产制造的所有文件的输出方法,包括:BOM.坐标.锡膏层.GERBER.钻孔.丝印和装配图 一.KiCAD导出BOM KiCAD导出BOM有以下几种办法: 注意: ...

  5. 【JS学习】慕课网7-23编程练习 有关字符串数组

    要求:1.显示打印的日期. 格式为类似“2014年03月21日 星期三” 的当前的时间.2.计算出该班级的平均分(保留整数).同学成绩数据如下:"小明:87; 小花:81; 小红:97; 小 ...

  6. 【Luogu】【关卡2-10】分治算法(2017年10月)

    任务说明:将大问题拆分为小问题,分而治之,各个击破,然后在合并回来. 取余运算||快速幂 幂次方 逆序对 南蛮图腾

  7. spring security学习总结

    这几天一直在学习spring security的相关知识.逛各大论坛,看相关api与教学视频,获益良多! 简介 Spring Security是为基于Spring的企业应用系统提供声明式的安全访问控制 ...

  8. Image另存为其他格式

    string imgPath = @"C:\ZHM01000001400001-01 青龙 白虎.tif"; Image img = Image.FromFile(imgPath) ...

  9. 【leetcode】538. Convert BST to Greater Tree

    题目如下: Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the orig ...

  10. 【leetcode】962. Maximum Width Ramp

    题目如下: Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. ...