本人使用的为junit4进行测试

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

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

控制层(action层)使用@Controller

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

持久层(Dao层)使用@Repository

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

详细介绍也可查看链接

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

junit测试

package com.example.thread.test;

import java.util.List;

import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext; import com.carrefour.dao.CheckP4AccountDao;
import com.carrefour.model.SimpleModel; public class CheckAccountThTest {
  
private CheckP4AccountDao checkP4AccountDao ; @Before
public void init() throws Exception {
      // 加载spring容器,此种方式为绝对路径方式,spring配置文件在WEB-INF下
// ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{
// "E:\\Workspaces\\MyEclipse 8.6\\carrefour\\WebRoot\\WEB-INF\\spring-servlet.xml"
// });
      // 加载spring容器,此种方式为相对路径,spring配置文件在WEB-INF下
ApplicationContext context = new FileSystemXmlApplicationContext(
"WebRoot/WEB-INF/spring-servlet.xml"
);
     // 获取Dao层实现类的Bean,此处注意,由于使用的为注解形式,spring会默认将类的首字母小写作为Bean的名称
      
checkP4AccountDao = (CheckP4AccountDao) context.getBean("checkP4AccountDaoImpl");
}
@Test
public void testAll() throws Exception{
    // 此处可以根据实际情况调用dao层的方法
List<SimpleModel> list = checkP4AccountDao.getAllRegion();
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. monkeyrunner总结

    device=MonkeyRunner.waitForConnection()   //手机连接 result = device.takeSnapshot()    //截图 result.write ...

  2. CAEmitterLayer实现粒子效果

    在iOS 5中,苹果引入了一个新的CALayer子类叫做CAEmitterLayer.CAEmitterLayer是一个高性能的粒子引擎,被用来创建实时例子动画如:烟雾,火,雨等等这些效果. CAEm ...

  3. Android getResources的作用和须要注意点

    今天做一个Android的文件管理器,里面用到非常多的地方用到了getResources. Drawable currentIcon = null; currentIcon = getResource ...

  4. CAsyncSocket

    本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 它是一个异步非阻塞Socket封装类,CAsyncSocket::Create()有一个参数指明了你想要处理哪些Socket事 ...

  5. VCS仿真查看coverage

    VCS仿真查看coverage Design compiler & simulate #!/bin/csh -f setenv NOVAS_HOME /EDASynopsys/verdi3-I ...

  6. OD: Exploit Me - Overwrite Nearby Varible

    实验代码: #include<stdio.h> #include<string.h> #define PASSWORD "1234567" int veri ...

  7. wed网页开发面试笔试必备小知识

    HTML中行内元素与块级元素的区别: 在标准文档流里面,块级元素具有以下特点: ①总是在新行上开始,占据一整行: ②高度,行高以及外边距和内边距都可控制: ③宽带始终是与浏览器宽度一样,与内容无关: ...

  8. 使用DataSet数据集删除记录

    使用DataSet删除记录和使用DataSet更新记录非常的相似,DataSet删除记录的步骤如下所示. q  创建一个Connection对象. q  创建一个DataAdapter对象. q  初 ...

  9. explicit 只对构造函数起作用,用来抑制隐式转换。

    class A { private: int a; public: A(int x) :a(x){} void display(){ cout << a << endl; } ...

  10. linux int to string 方法

    最近从windows 移植程序的时候发现to_string()函数在linux 中不能用,网上找了几种方法,觉得使用stringstream对象来实现类型转化比较好一点. 你只需在你工程中加入下面的t ...