整合Junit

  • 导入jar包
    基本 :4+1
    测试:spring-test-5.1.3.RELEASE.jar
  1. 让Junit通知spring加载配置文件
  2. 让spring容器自动进行注入
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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;
    (SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:applicationContext.xml")
    public class {
    @Autowired
    private AccountService accountService;
    @Test
    public void demo() {
    accountService.transfer("jack", "rose", 1000);
    }
    }

整合web

  • 导入jar
    spring-web-5.1.3.RELEASE.jar
  1. tomcat启动加载配置文件
    servlet –> init(ServletConfig) –> 2
    filter –> init(FilterConfig) –> web.xml注册过滤器自动调用初始化
    listener –> ServletContextListener –> servletContext对象监听【】
    spring提供监听器 ContextLoaderListener –> web.xml 大专栏  spring入门-整合junit和webtener>….

    如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml
  2. 确定配置文件位置,通过系统初始化参数
    ServletContext 初始化参数 web.xml

    <context-param>
    <param-name>contextConfigLocation
    <param-value>classpath:applicationContext.xml

代码

  1. 修改web.xml

    1
    2
    3
    4
    5
    6
    7
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  2. servlet中调用

    1
    2
    3
    4
    5
    // 从application作用域(ServletContext)获得spring容器
    //方式1: 手动从作用域获取
    ApplicationContext applicationContext = (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    //方式2:通过工具获取
    ApplicationContext apppApplicationContext2 = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

spring入门-整合junit和web的更多相关文章

  1. Spring Boot 整合Junit和redis

    14. Spring Boot整合-Junit 目标:在Spring Boot项目中使用Junit进行单元测试UserService的方法 分析: 添加启动器依赖spring-boot-starter ...

  2. Spring Test 整合 JUnit 4 使用

    这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了.因为所有的Bean都需要在applicationContext.xml中加载好,之后再通过@Resource去取得.如果每次 ...

  3. Spring Test 整合 JUnit 4 使用总结

    转自:https://blog.csdn.net/hgffhh/article/details/83712924 这两天做Web开发,发现通过spring进行对象管理之后,做测试变得复杂了.因为所有的 ...

  4. Spring 框架整合JUnit单元测试

    // 整合之前 public class Demo{ @Test public void fun(){ // 获取工厂,加载配置文件 ApplicationContext ac = new Class ...

  5. Spring Boot 整合 Thymeleaf 完整 Web 案例

    Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...

  6. SpringMVC+Spring+MyBatis整合完整版Web实例(附数据)

    最近段时间正在学习Spring MVC和MyBatis的一些知识.自己也在网络上面找了一些例子来练习.但是都不是很完整.所以,今天,自己也抽空写了个完成的关于Spring MVC + Spring + ...

  7. Spring框架整合JUnit单元测试

    1. 为了简化了JUnit的测试,使用Spring框架也可以整合测试 2. 具体步骤 * 要求:必须先有JUnit的环境(即已经导入了JUnit4的开发环境)!! * 步骤一:在程序中引入:sprin ...

  8. 09.spring框架整合junit

    在正常的实际开发中都是按照上面这种方式来进行管理的.

  9. Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)

    参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...

随机推荐

  1. 获取cell上按钮事件

    原由:点击cell上的按钮,无法获取button对应的cell位置 //获取按钮上层控件,也就是cell本身 AccountCell *cell= (AccountCell *)[按钮名称 super ...

  2. JavaScript 的 URL 对象是什么?

    如果我们自己编写从URL中分析和提取元素的代码,那么有可能会比较痛苦和麻烦.程序员作为这个社会中最“懒”的群体之一,无休止的重复造轮子必然是令人难以容忍的,所以大多数浏览器的标准库中都已经内置了URL ...

  3. @interface 注解详解

    转:http://www.cnblogs.com/xdp-gacl/p/3622275.html 只为成功找方法,不为失败找借口! Java基础加强总结(一)——注解(Annotation) 一.认识 ...

  4. 可用的 .net core 支持 RSA 私钥加密工具类

    首先说明 MS并不建议私钥加密,而且.net 于安全的考虑,RSACryptoServiceProvider类解密时只有同时拥有公钥和私钥才可以,原因是公钥是公开的,会被多人持有,这样的数据传输是不安 ...

  5. ant design for vue 上传文件

    1.使用customRequest customRequest 通过覆盖默认的上传行为,可以自定义自己的上传实现 Function 定义customRequest,之前定义action行为会被覆盖,可 ...

  6. LoadRunner 工具使用

    LoaderRunner 第一天 1.1 性能测试基础 ​ 服务器端性能测试 1.1 什么是性能测试的本质 基于协议模拟用户发出请求(业务的模拟), 对服务器形成一定的负载,来测试服务器的性能指标是否 ...

  7. 不同SQL数据库之间表数据的实时同步-发布与订阅

    https://blog.csdn.net/rand_muse/article/details/81326879 上述文章中,如果是实时同步,选择  事务发布即可 在快照代理 那里,不需要勾选  运行 ...

  8. linux打开和关闭端口

    查看哪些端口被打开  netstat -anp 关闭端口号:iptables -A INPUT -p tcp -drop 8080 -j DROP 打开端口号:iptables -A INPUT -p ...

  9. python后端向前台返回字节流文件

    python后端向前台返回字节流文件,浏览器访问地址自动下载文件: from django.http.response import StreamingHttpResponse # 要下载的文件路径 ...

  10. EMCCD

    EMCCD 即电子倍增CCD,是探测领域内灵敏度极高的一种高端光电探测产品. 在光子探测领域的应用发展对探测器灵敏度的要求不断提高,EMCCD (Electron-Multiplying CCD)技术 ...