整合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. HTML学习的一些笔记

    为什么会出现HTML这个标记语言 超文本标记语言 HyperText Markup Language HTML是超文本标记语言,标准通用标记语言的一个应用. "超文本"就是指页面内 ...

  2. 论文翻译——Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection

    Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection 动态池和展开递归自动编码器的意译检测 论文地 ...

  3. spark mllib lda 中文分词、主题聚合基本样例

    github https://github.com/cclient/spark-lda-example spark mllib lda example 官方示例较为精简 在官方lda示例的基础上,给合 ...

  4. 如何在linux中运行sql文件

    1.在linux中进入sql命令行 mysql -u root -p   输入密码 2.假设home下面有a.sql文件 先得use databasename,要不会报错 “No Database S ...

  5. mybatis: Invalid bound statement (not found)即使调试源码也很难注意的坑

    需要改成 注意: 这个是不能一次性建立多级目录的 这个才是ok的

  6. 108)PHP分页显示

    一个代码页的链接:https://www.cnblogs.com/mmykdbc/p/6688460.html 首先一个简单的代码展示: 目录关系: 数据库表格展示: 结果展示:   然后  代码展示 ...

  7. PAT甲级——1027 Colors in Mars

    1027 Colors in Mars People in Mars represent the colors in their computers in a similar way as the E ...

  8. layui表格自动对齐

    正常情况table应该是这样展示的 但是却展示成了这样 格子没对齐,找了半天原因发现是在table.render中的cols参数多了一个"," 不细心一点真看不出来 正常结构应是 ...

  9. 吴裕雄--天生自然 R语言开发学习:使用键盘、带分隔符的文本文件输入数据

    R可从键盘.文本文件.Microsoft Excel和Access.流行的统计软件.特殊格 式的文件.多种关系型数据库管理系统.专业数据库.网站和在线服务中导入数据. 使用键盘了.有两种常见的方式:用 ...

  10. SQL语句简单应用(未完)

    简介:   SQL(structured query language)结构化查询语句,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系型数据库系统,同时也是数据库脚本文件的扩展名 ...