spring整合(Junit、web)
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)的更多相关文章
- spring整合junit报错
1.Could not autowire field: private javax.servlet.http.HttpServletRequest 参考:https://www.cnblogs.com ...
- Spring整合junit测试
本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...
- 阶段3 2.Spring_06.Spring的新注解_8 spring整合junit完成
Junit的核心Runner在执行的时候不会创建容器.同时它字节码文件,也改不了 spring整合junit 想办法把junit里面的不能加载容器的main方法换掉.从而实现创建容器.有了容器就可以实 ...
- Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)
参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...
- SSM框架中测试单元的使用,spring整合Junit
测试类中的问题和解决思路 3.1.1 问题 在测试类中,每个测试方法都有以下两行代码: ApplicationContext ac = new ClassPathXmlApplicatio ...
- 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)
我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclips ...
- Spring整合Junit框架
一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...
- Spring整合Junit框架进行单元测试Demo
一.开发环境 eclipse版本:4.6.1 maven版本:3.3.3 junit版本:4.12 spring版本:4.1.5.RELEASE JDK版本:1.8.0_111 二.项目结构 图 三. ...
- Spring整合JUnit spring静态对象属性的注入
package cn.itcast.d_junit4; import org.junit.Test; import org.junit.runner.RunWith; import org.sprin ...
- Spring整合JUnit框架进行单元测试代码使用详解
一.Spring提供的JUnit框架扩展: 1. AbstractSpringContextTests:spring中使用spring上下文测试的Junit扩展类,我们一般不会使用这个类来进行单元 ...
随机推荐
- 使用Java8中的Optional类来消除代码中的null检查
简介 Optional类是Java 8新增的一个类,Optional 类主要解决的问题是臭名昭著的空指针异常(NullPointerException). —— 每个 Java 程序员都非常了解的异常 ...
- 国内几大seo高手(夫唯,王通,久久)的技术分析
http://www.wocaoseo.com/thread-146-1-1.html 目前学习seo的人越来越多了,这种技术的普及和推广也在不断的扩大,先进的好的培训机构不断涌现,很多高水平老师都在 ...
- 2个案例带你快速实现Response返回值
今天先来学习一下Response的相关知识. 所有返回前台的内容其实都应该是Response的对象或者其子类,我们看到如果返回的是字符串直接可以写成return u'字符串内容'的形式,但是其实这个字 ...
- [ASP.NET Core开发实战]基础篇04 主机
主机定义 主机是封闭应用资源的对象. 设置主机 主机通常由 Program 类中的代码配置.生成和运行. HTTP项目(ASP.NET Core项目)创建泛型主机: public class Prog ...
- systemctl 如何启动、关闭、启用/禁用服务
启动服务:systemctl start xxx.service 关闭服务:systemctl stop xxx.service 重启服务:systemctl restart xxx.service ...
- webdriver入门之环境准备
1.安装ruby 下载ruby的安装包,很简单,不解释.装好之后打开cmd输入以下命令验证是否安装成功 ruby -v 2.安装webdriver 确保机器联网,用gem命令安装是在有网络的情况下进行 ...
- 【HttpRunner v3.x】笔记—6. 测试用例-teststeps-RunRequest
之前我们了解了config里的各项参数,今天来了解另一个重要部分--teststeps,在这之前,先看看测试用例的分层模型. 一.测试用例分层模型 一个testcase里(就是一个pytest格式的P ...
- Oracle (实例名/服务名)SID和Service_Name的区别
可以简单的这样理解:一个公司比喻成一台服务器,数据库是这个公司中的一个部门.1.SID:一个数据库可以有多个实例(如RAC),SID是用来标识这个数据库内部每个实例的名字,就好像一个部门里,每个人都有 ...
- 08_线程间通信 ITC
1.线程间的通信方式 1.共享全局变量 2.线程队列(queue) 3.信号量(Semaphore) 2.共享全局变量进行通信 from threading import Thread import ...
- 哦!这该死的 C 语言
前言 C 语言是一门抽象的.面向过程的语言,C 语言广泛应用于底层开发,C 语言在计算机体系中占据着不可替代的作用,可以说 C 语言是编程的基础,也就是说,不管你学习任何语言,都应该把 C 语言放在首 ...