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. All in One 你想知道的 hacker 技术都在这里

    作者:HelloGitHub-小鱼干 hacker 这个词,大多数理解为黑客,而维基百科对其的定义为--黑客(Hacker)是指对设计.編程和计算机科学方面具高度理解的人,在本文中 hacker 主要 ...

  2. C#-接口(Interface)详解

    定义 在 C# 语言中,类之间的继承关系仅支持单重继承,而接口是为了实现多重继承关系设计的.一个类能同时实现多个接口,还能在实现接口的同时再继承其他类,并且接口之间也可以继承.无论是表示类之间的继承还 ...

  3. 那些jdk中坑你没商量的方法

    前言:jdk作为我们每天必备的调用类库,里面大量提供了基础类供我们使用.可以说离开jdk,我们的java代码寸步难行,jdk带给我们的便利可谓是不胜枚举,但同时这些方法在使用起来也存在一些坑,如果不注 ...

  4. MySQL集群搭建方案(PXC)

    服务器快过期了,清一点库存,把运维这块的知识复习下 为什么要搭MySQL集群 技术层面上,传统的单节点数据库,万一宕机了,就凉凉了.容灾性能差.抗并发能力有限,数据量大的时候查询有瓶颈.学习层面上,作 ...

  5. win10安装JDK cmd中可以运行java,但不能用javac,解决方案

    win10安装JDK cmd中可以运行java,但不能用javac 网上教程(1)新建->变量名"JAVA_HOME",变量值"C:\Java\jdk1.8.0_0 ...

  6. Android开发之第三方推送JPush极光推送知识点详解 学会集成第三方SDK推送

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 下面是一些知识点介绍,后期将会带领大家进行代码实战: 一.Android实现推送方式解决方案: 1.推 ...

  7. Unity碰撞消息(OnCollisionXXXX)和触发消息(OnTriggerXXXX)的调用情境

    MonoBehaviour中的消息非常多,一共有62个! 除了必须关注的脚本生命周期的一系列函数外,还有其他两组比较常混淆的消息:碰撞和触发. 按3D和2D物体区分,又分为碰撞:Collision.C ...

  8. 【好文分享】为什么强烈禁止开发人员使用isSuccess作为变量名

    原文来自阿里云hollies:https://developer.aliyun.com/article/701413   简介: 在日常开发中,我们会经常要在类中定义布尔类型的变量,比如在给外部系统提 ...

  9. 16_Python设计模式

    1.设计模式概述 1.设计模式代表了一种最佳的实践,是被开发人员长期总结,用来解决某一类问题的思路方法,这些方法保证了代码的效率也易于理解 2.设计模式类型:根据23种设计模式可以分为三大类     ...

  10. rank,dense_rank和row_number函数区别

    我对技术一般抱有够用就好的态度,一般在网上或者书上找了贴合的解决方案,放到实际中发现好用就行了,不再深究,等出了问题再说. 因此,我对Oracle中中形成有效序列的方法集中在rownum,row_nu ...