使用Spring的隐式注解和装配以及使用SpringTest框架
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 {
}

并且可以使用excludeFilters 来排除不需要的组件
4.加入SpringTest的框架
1) 写入@RunWith(SpringJUnit4ClassRunner.class) 由SpringJunit4ClassRunner 来替我们创建容器
首先Junit 会根据我们的SpringJUnit4ClassRunner.class 找到 TestContext 然后创建 TestContextManager 然后将TestExecutionListenner 加入到 容器中
而下面有个AbstractTestExecutionListener 可以创建我们所需要的Listenner 可以在下面的代码中看到创建Listenner

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

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框架的更多相关文章
- Spring基础篇——bean的自动化装配
上篇博文讲Spring的IOC容器时说道,虽然容器功能强大,但容器本身只是个空壳,需要我们主动放入装配对象,并告诉它对象之间的协作关系,然后容器才能按照我们的指示发挥它的魔力,完成装配bean的使命. ...
- SpringInAction--自动化装配Bean(隐式装配)
关于Bean的介绍就具体不多介绍了,,, Spring在配置时候有三种方案可选 1.在xml中进行显示配置 2.在java中进行显示配置 3.隐式的Bean发现机制和自动装配 今天学习的就是自动化装配 ...
- (转)java之Spring(IOC)注解装配Bean详解
java之Spring(IOC)注解装配Bean详解 在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring Security OAuth2 Demo —— 隐式授权模式(Implicit)
本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_impilit_pattern.html 写在前面 在文章OAuth 2.0 概念及授权流程梳 ...
- Spring基于注解自动装配
前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...
- Spring IOC之基于注解的容器配置
Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...
随机推荐
- 怎样查看当前QQ的版本号是多少
怎样查看当前QQ的版本号是多少.. -------------- -------------
- 9.19.3 反射和Properties(重要)
dbinfo.properties文件中的内容: driver oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@192.168.1. ...
- 递归调用里的性能问题(js)
说明 这是在codewars.com上刷的一道js练习题,在此做个记录 问题描述 The Fibonacci sequence is traditionally used to explain tre ...
- 传参时Url中有中文报错
url中如果传了中文,浏览器会报错:The header content contains invalid characters. 原因:浏览器自动把这个url进行decodeURIComponent ...
- NCS8801S芯片RGB/LVDS转EDP功能简介
NCS8801S RGB/LVDS-to-eDP Converter (1/2/4-lane eDP) Features --Embedded-DisplayPort (eDP) Output 1/2 ...
- AVAssetWriter 硬编码bug解决
一.需求 直播助手在录屏过程中,产品要求跟随用户手机屏幕旋转,录屏的视频跟随旋转 二.实施方案 目前触手录,iTools PC端均已经实现该功能,并且该功能只适配iOS9和iOS10系统.猜测实现方案 ...
- informatica9 安装下载,安装教程 介质(文章和视频教程)(csdn讲师:Array)
Informatica学习: 参考文献:视频参考地址:http://edu.csdn.net/course/detail/5034,ETL之informatica9通关班(第二期) 1.安装介质的获 ...
- HAproxy功能配置
author:JevonWei 版权声明:原创作品 haproxy配置文档 https://cbonte.github.io/haproxy-dconv/ 环境 前端HAProxy 172.16.25 ...
- 后端路由项目由 gulp 改为 webpack 的踩坑实录
前言 公司有个后端路由的项目是用 gulp 作为前端自动化构建工具,最近学习了一下 webpack,深感其强大,一狠心将其改成了 webpack 构建,以下是踩坑实录. gulp 先来说说原来的架构. ...
- HTTP请求范例
package com.grefr.basemethod; /*JAVA发送HTTP请求,返回HTTP响应内容,实例及应用 博客分类: JAVA实现 Java.netBeanJDKApache . J ...