本人使用的为junit4进行测试

spring-servlet.xml中使用的为注解扫描的方式

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  15.  
  16. <context:annotation-config />
  17.  
  18. <!-- 自动扫描所有注解该路径 -->
  19. <context:component-scan base-package="com.carrefour" />
  20. <!--
  21. 主要作用于@Controller,激活该模式, 下面是一种简写形式,完全可以手动配置替代这种简写形式,
  22. 它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,
  23. 是spring MVC为@Controllers分发请求所必须的
  24. -->
  25. <mvc:annotation-driven />

控制层(action层)使用@Controller

业务层(service层)使用@Service,

持久层(Dao层)使用@Repository

也可以三层统一使用@Component也是可以的,但推荐使用上方的方式,便于区分

详细介绍也可查看链接

http://blog.csdn.net/yi3040/article/details/6447289

junit测试

  1. package com.example.thread.test;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.springframework.context.ApplicationContext;
  9.  
  10. import com.carrefour.dao.CheckP4AccountDao;
  11. import com.carrefour.model.SimpleModel;
  12.  
  13. public class CheckAccountThTest {
  14.   
  15. private CheckP4AccountDao checkP4AccountDao ;
  16.  
  17. @Before
  18. public void init() throws Exception {
          // 加载spring容器,此种方式为绝对路径方式,spring配置文件在WEB-INF下
  19. // ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{
  20. // "E:\\Workspaces\\MyEclipse 8.6\\carrefour\\WebRoot\\WEB-INF\\spring-servlet.xml"
  21. // });
          // 加载spring容器,此种方式为相对路径,spring配置文件在WEB-INF下
  22. ApplicationContext context = new FileSystemXmlApplicationContext(
  23. "WebRoot/WEB-INF/spring-servlet.xml"
  24. );
  25.      // 获取Dao层实现类的Bean,此处注意,由于使用的为注解形式,spring会默认将类的首字母小写作为Bean的名称
          
  26. checkP4AccountDao = (CheckP4AccountDao) context.getBean("checkP4AccountDaoImpl");
  27. }
  28. @Test
  29. public void testAll() throws Exception{
        // 此处可以根据实际情况调用dao层的方法
  30. List<SimpleModel> list = checkP4AccountDao.getAllRegion();
  31. System.out.println(list.size());
         System.out.println(finish);
      }

测试方法还有很多中,此种方法个人感觉较好用和理解,如有喜欢可以收藏

junit4测试 Spring MVC注解方式的更多相关文章

  1. [转]spring mvc注解方式实现向导式跳转页面

    由于项目需要用到向导式的跳转页面效果,本项目又是用spring mvc实现的,刚开始想到用spring 的webflow,不过webflow太过笨重,对于我们不是很复杂的跳转来说好像有种“杀鸡焉用牛刀 ...

  2. Spring mvc注解方式使用事务回滚

    项目名:1ma1ma jdbc.xml <bean  id="dataSource" class="org.apache.commons.dbcp.BasicDat ...

  3. 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法

    spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...

  4. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  5. spring mvc 注解@Controller @RequestMapping @Resource的详细例子

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  6. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  7. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  8. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  9. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

随机推荐

  1. [React] React Router: Route Parameters

    A router library is no good if we have to hardcode every single route in our application. In this le ...

  2. SURF特征

    了解了SIFT特征后,来学习SURF特征. 虽说是SIFT的一个变种,可是跟SIFT还是有差别的 差别有例如以下: 1.尺度空间的构建(近似)不同. 2.同意尺度空间多层图像同一时候被处理 3.特征点 ...

  3. iOS蓝牙4.0协议简单介绍

    iOS开发蓝牙4.0的框架是CoreBluetooth,本文主要介绍CoreBluetooth的使用,关于本文中的代码片段大多来自github上的一个demo,地址是myz1104/Bluetooth ...

  4. 解决从github下载web的源代码部署到eclipse的问题

    2015年6月2日 天气晴 github官网:https://github.com/ 以下以pdf.js作为案例说明:https://github.com/mozilla/pdf.js 1).点击案例 ...

  5. uploadify3.1 参数 中文详解

    langFile: 'http://www.static-xxx.nu/uploader/uploadifyLang_en.js',//语言包的路径,能设置所有的提示文字 swf: 'http://w ...

  6. 网页JavaScript1

    DOM的操作 windows对象操作 属性: opener,打开当前窗口的源窗口,首次启动 是null. dialogArgument,对话框的返回值 子对象: history , location ...

  7. MVC4 数据库连接字串

    1.SQL Server <add name="DBEntities" connectionString="Data Source=.;Initial Catalo ...

  8. Sql 2012 OFFSET / FETCH NEXT BUG

    上个星期,测试发现了一个分页的bug--- 无论怎么分页数据的一样.我们所有的分页都是用EF 分页,为什么只有一个模块的分页有问题呢? 后来跟了下sql语句,发现用到是sql2012的新分页方式 OF ...

  9. C#字节byte类型读取与写入

    //创建文件 //File.Create(@"D:\a.txt"); //Console.WriteLine("创建成功"); ////删除文件 //File. ...

  10. polygonZM---> poliygon

    ArcToolbox > Conversion Tools > To Shapefile > Feature Class To Shapefile (multiple)   Clic ...