1、整合Junit

(1)整合前的测试类代码

public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =(AccountService) applicationContext.getBean("accountService");
accountService.transfer("zhai","zhang",10);
}
}

需要先加载配置文件,获得spring容器,然后从容器中获得对象即可调用相应的类中的方法。

(2)整合后的代码:

需要先导入jar包:

基础包:4+1

测试包:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test {
@Autowired//与JUnit整合,不需要在spring的xml配置文件中配置扫描
private AccountService accountService;
public static void main(String[] args) { ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =(AccountService) applicationContext.getBean("accountService");
accountService.transfer("zhai","zhang",10);
}
}

加载配置文件:

@ContextConfiguration(locations = "classpath:applicationContext.xml")

classpath 的作用是告诉我们配置文件的位置是src目录下。

2、整合web

(1)导入jar包:

(2)tomcat启动时加载配置文件的方式:

servlet init(ServletConfig) <load-on-startup>

filter init(FilterConfig) web.xml注册过滤器自动调用初始化

listener ServletContextListenter ServletContext 对象监听(spring运用的是这个)

spring提供监听器 ContextLoaderListener web.xml

(3)对web.xml文件进行配置(加载配置文件):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--确定配置文件的位置,默认情况在WEB-INF目录下-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置spring监听器,加载配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

(4)从servletContext作用域获得spring容器

public class TestServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { } protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
//获得spring容器,手动从applicationContext作用域获取
ApplicationContext applicationContext=
(ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//通过工具获取
ApplicationContext applicationContext1=
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
AccountService accountService =(AccountService) applicationContext.getBean("accountService");
accountService.transfer("zhai","zhang",10);
}
}

获取ApplicationContext 的对象有两种方式。

书写一个页面点击后访问servlet:

<%--
Created by IntelliJ IDEA.
User: zhai
Date: 2020/4/17/0017
Time: 10:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/test">获得spring容器</a>
</body>
</html>

spring整合(Junit、web)的更多相关文章

  1. spring整合junit报错

    1.Could not autowire field: private javax.servlet.http.HttpServletRequest 参考:https://www.cnblogs.com ...

  2. Spring整合junit测试

    本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...

  3. 阶段3 2.Spring_06.Spring的新注解_8 spring整合junit完成

    Junit的核心Runner在执行的时候不会创建容器.同时它字节码文件,也改不了 spring整合junit 想办法把junit里面的不能加载容器的main方法换掉.从而实现创建容器.有了容器就可以实 ...

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

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

  5. SSM框架中测试单元的使用,spring整合Junit

    测试类中的问题和解决思路   3.1.1     问题 在测试类中,每个测试方法都有以下两行代码: ApplicationContext ac = new ClassPathXmlApplicatio ...

  6. 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)

    我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclips ...

  7. Spring整合Junit框架

    一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...

  8. Spring整合Junit框架进行单元测试Demo

    一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...

  9. Spring整合JUnit spring静态对象属性的注入

    package cn.itcast.d_junit4; import org.junit.Test; import org.junit.runner.RunWith; import org.sprin ...

  10. Spring整合JUnit框架进行单元测试代码使用详解

    一.Spring提供的JUnit框架扩展:   1. AbstractSpringContextTests:spring中使用spring上下文测试的Junit扩展类,我们一般不会使用这个类来进行单元 ...

随机推荐

  1. Federated Learning: Challenges, Methods, and Future Directions

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! arXiv:1908.07873v1 [cs.LG] 21 Aug 2019 Abstract 联邦学习包括通过远程设备或孤立的数据中心( ...

  2. mr原理简单分析

    背景 又是一个周末一天一天的过的好快,今天的任务干啥呢,索引总结一些mr吧,因为前两天有面试问过我?我当时也是简单说了一下,毕竟现在写mr程序的应该很少很少了,废话不说了,结合官网和自己理解写起. 官 ...

  3. linux系统学习

    一.linux系统如何配置环境变量 1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件 ...

  4. Docker学习笔记-Dockerfile文件详解

    什么是Dockerfile? Docker中有个非常重要的概念叫做--镜像(Image).Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序.库.资源.配置等文件外,还包含了一些为运 ...

  5. 腾讯大牛教你简单的自动化测试模型(Python+Selenium)

    今天讲解简单的自动化测试模型,对于刚接触自动化测试的同学,由于没有编程语言的基础,是搞不懂代码里面的函数.封装.包以及其他概念,只是了解字符串.数组.元组及字典这种最基本的名词,更不懂自动化测试框架了 ...

  6. 水仙花数的条件:1.是一个三位数,2.个百千位数字的3次方加起来的和等于当前的三位数。如果,想要完美一点可以在外部加while循环

    #!/usr/bin/env python# -*- coding: utf-8 -*-print("请输入三位数:")num = input()# 定义常量SumNum = 0# ...

  7. 如何让Web程序在点击按钮后出现如执行批处理程序般的效果

    在cli程序中,输入命令得到连续的输出已经是一种进度而美观的页面交互形式,好比下图: 而web程序里也有类似的场景,比如执行一个耗时任务,除了显示出等待图标外,用户还希望把执行的状态及时显示出来.如下 ...

  8. 登录SQL Server服务器时的服务器名称

    在本地访问Microsoft SQL Server 2008服务器时,首先就是要填写正确的服务器名称.经测试,该名称可以有多种写法,具体如下: 写法一: X\sqlexpress(X即计算机名) 写法 ...

  9. Redis源码笔记--服务器日志和函数可变参数处理server.c

    前言 Redis源码中定义了几个和日志相关的函数,用于将不同级别的信息打印到不同的位置(日志文件或标准输出,取决于配置文件的设置),这些函数的定义位于 server.h 和server.c 文件中,包 ...

  10. Appium之启动第一个App

    搭建appium自动化环境真是各种问题呀. 如何启动在真机上启动App? 执行操作:操作Android真机上打开手机淘宝app,并搜索“熊猫”. 脚本源码如下: from appium import ...