整合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. C++ malloc函数

    malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑定真正的内 ...

  2. python tricks 01

    01: 考察range/sort/lambda 对以下数据进行排序 原数据: [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] 目标数据: [0, -1, 1, -2, 2 ...

  3. springmvc register过程

    福建SEO:首先在AbstractHandlerMethodMapping中,在afterPropertiesSet这个钩子函数中,先初始化handlerMethods. 在detectHandler ...

  4. sqlite如何避免重复建表(获取已经存在的表)

    找到已经存在的所有表,手动判断是否需要建表 SELECT name FROM SQLITE_MASTER WHERE type='table'ORDER BY name" 建表时sqlite ...

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

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

  6. python处理nii格式文件

    网上已经有很多代码了,但是注释的都不全,看起来很费解,我自己加了一些注释,重新发出来,尽可能的通俗易懂 读取前需要先安装库 pip install nibabel pip install matplo ...

  7. CentOS下MySQL忘记root密码解决方法【亲测】

    1.修改MySQL的登录设置: # vim /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/m ...

  8. SAP 配置表记录创建人/创建日期/创建时间/更改人/更改日期/更改时间

    在实际开发需求中,为了使客制功能具有灵活的可配置性,通常采用开发功能+配置表的形式处理.有些客制的配置功能需要追溯到谁在什么时候增加了什么配置,或者谁在什么时候更改了什么位置,配置表的Log功能就显得 ...

  9. 吴裕雄--天生自然C语言开发:存储类

    { int mount; auto int month; } { register int miles; } #include <stdio.h> /* 函数声明 */ void func ...

  10. 68.26-95.44-99.74 rule|empirical rule

    6.3 Working with Normally Distributed Variables As illustrated in the previous example, the 68.26-95 ...