使用Spring的隐式注解和装配以及使用SpringTest框架

时间:2022-09-02 00:04:16

SpringTestConfiguration

1.加入jar 包spring-test-4.3.9.RELEASE.jar

2.写基本的Component

注意级联状态下  需要给需要调用的属性加入getter方法

 package com.myth.spring.annotion.component;

 import org.springframework.stereotype.Component;

 @Component
public class Person {
private String name = "Sally";
private int age = 26;
private Car car ; public void travel(Car car) {
System.out.println(name + " go to travel by "+car.getCarName() + " when at "+age );
}
}
 package com.myth.spring.annotion.component;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Car {
private String carName = "Q5"; private String carBrand = "Audi"; private int carPrice = 1000000; public void run() {
System.out.println("The car :"+carName+"'s brand is "+carBrand+" and the price is "+carPrice);
} //若级联的类需要调用类的方法时 需要加入get 方法
@Autowired
public String getCarName() {
return carName;
}
}

3.加入 ComponentScan的配置 basePackages 可以扫描多个包

 package com.myth.spring.annotion.configuration;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//加入Configuration
@Configuration
//加入ComponentScan并配置basepackage
@ComponentScan(basePackages="com.myth.spring.annotion")
public class TravelConfiguration {
}

使用Spring的隐式注解和装配以及使用SpringTest框架

并且可以使用excludeFilters 来排除不需要的组件

4.加入SpringTest的框架

1) 写入@RunWith(SpringJUnit4ClassRunner.class) 由SpringJunit4ClassRunner 来替我们创建容器

首先Junit  会根据我们的SpringJUnit4ClassRunner.class 找到  TestContext   然后创建 TestContextManager  然后将TestExecutionListenner 加入到  容器中

而下面有个AbstractTestExecutionListener 可以创建我们所需要的Listenner 可以在下面的代码中看到创建Listenner

使用Spring的隐式注解和装配以及使用SpringTest框架

  • TestContext:它封装了运行测试用例的上下文;
  • TestContextManager:它是进入 Spring TestContext 框架的程序主入口,它管理着一个 TestContext 实例,并在适合的执行点上向所有注册在 TestContextManager 中的 TestExecutionListener 监听器发布事件:比如测试用例实例的准备,测试方法执行前后方法的调用等。
  • TestExecutionListener:该接口负责响应 TestContextManager 发布的事件。

使用Spring的隐式注解和装配以及使用SpringTest框架

protected void dirtyContext(TestContext testContext, HierarchyMode hierarchyMode) {
testContext.markApplicationContextDirty(hierarchyMode);
testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}

2) 写入@ContextConfiguration(classes=TravelConfiguration.class) 来识别配置的类

3) 用autowired 来表示自动装箱机制。

TestTravel

 package com.myth.spring.annotion.test;

 import static org.junit.Assert.assertNotNull;

 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; import com.myth.spring.annotion.component.Car;
import com.myth.spring.annotion.component.Person;
import com.myth.spring.annotion.configuration.TravelConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TravelConfiguration.class)
public class TestTravel {
@Autowired
private Person person;
@Autowired
private Car car; @Test
public void test() {
person.travel(car);
} }

使用Spring的隐式注解和装配以及使用SpringTest框架的更多相关文章

  1. Spring基础篇——bean的自动化装配

    上篇博文讲Spring的IOC容器时说道,虽然容器功能强大,但容器本身只是个空壳,需要我们主动放入装配对象,并告诉它对象之间的协作关系,然后容器才能按照我们的指示发挥它的魔力,完成装配bean的使命. ...

  2. SpringInAction--自动化装配Bean(隐式装配)

    关于Bean的介绍就具体不多介绍了,,, Spring在配置时候有三种方案可选 1.在xml中进行显示配置 2.在java中进行显示配置 3.隐式的Bean发现机制和自动装配 今天学习的就是自动化装配 ...

  3. (转)java之Spring(IOC)注解装配Bean详解

    java之Spring(IOC)注解装配Bean详解   在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...

  4. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  6. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  7. Spring Security OAuth2 Demo —— 隐式授权模式(Implicit)

    本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_impilit_pattern.html 写在前面 在文章OAuth 2.0 概念及授权流程梳 ...

  8. Spring IOC之基于注解的容器配置

    Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...

  9. Spring学习日志之Bean的装配

    Spring容器负责创建应用程序中的bean并通过依赖注入来协调这些对象之间的关系.但是,作为开发人员,要告诉Spring需要创建哪些bean并且如何将其装配在一起.当描述bean如何装配时,Spri ...

随机推荐

  1. 耿丹CS16-2班第一次作业汇总

    第一次作业统计完成. 注:1.作业顺序:取最早交作业的前3名,依次拿5,2,1分,前提是作业质量较高,否则轮至下一名同学得分,其余同学得0分:2.作业情况:满10分,空一题扣2分,心得写得好的有额外加 ...

  2. jquery中隐藏div的几种方法

    //jQuery中的显示.隐藏方法 $("#id").show()://表示display:block,    $("#id").hide()://表示disp ...

  3. DP:炮兵阵地问题(POJ 1185)

    正确的打炮方式(大雾)(点我查看) 2015-08-21 问题是中文的,大家可以进去看看. 先说一个坑,这个问题我交了很多次,都没过,反正是WA到我烦了,都不知道哪里错了!!!怎么会有错,然后翻了一下 ...

  4. CentOS6 下Vim安装和配置

    1.系统只安装了vim-minimal,执行以下命令安装另外两个组件 yum install vim-common yum install vim-enhanced 2.安装ctags yum ins ...

  5. AVOS_百度百科

    AVOS_百度百科 AVOS 目录 公司产品 AVOS 是 YouTube 创始人 Chad Hurley 和 Steve Chen(陈士骏)创立的互联网公司.    编辑本段公司产品    产品包括 ...

  6. centos下安装与配置Apache方法

    下面以httpd-2.0.55.tar.gz版本为例,介绍Apache在Linux中的安装过程:1.解压和解包安装文件:gzip -d httpd-2.0.55.tar.gztar xvf httpd ...

  7. sourceTree每次拉取代码和提交代码都需要输入密码

    今天新安装的sourceTree导入项目,拉取代码的时候一直提示让我输入git密码,每次拉取和提交的时候都需要重新输入密码,甚是麻烦,在网上,搜索,解决办法五花八门,这里提供一种简单有效的方法供大家参 ...

  8. 使用FastJson进行对象和JSON转换属性命名规则为下划线和驼峰的问题

    public class AliPayParam { @JSONField(name="out_trade_no") private String outTradeNo; @JSO ...

  9. NPOI操作创建Excel

    一.下载NPOI类库 使用Nuget在线搜索NPOI,下载安装 二.代码开撸 var workBook = new HSSFWorkbook(); #region 设置样式 IFont font = ...

  10. 访问平安银行网站时出现证书问题 NET::ERR_CERT_SYMANTEC_LEGACY

    访问平安银行网站时出现证书问题 NET::ERR_CERT_SYMANTEC_LEGACY 查了资料说是 Google 取消了对 Symantec SSL 的信任,因为 Symantec 干了坏事1. ...