junit4测试 Spring MVC注解方式
本人使用的为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注解方式的更多相关文章
- [转]spring mvc注解方式实现向导式跳转页面
由于项目需要用到向导式的跳转页面效果,本项目又是用spring mvc实现的,刚开始想到用spring 的webflow,不过webflow太过笨重,对于我们不是很复杂的跳转来说好像有种“杀鸡焉用牛刀 ...
- Spring mvc注解方式使用事务回滚
项目名:1ma1ma jdbc.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDat ...
- 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法
spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- spring mvc 注解@Controller @RequestMapping @Resource的详细例子
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
随机推荐
- macos10.8.5原版系统dmg转iso
网上非常多资料说使用UltraISO打开macos10.8.5, 将InstallESD.dmg提取出来, 然后再用UltraISO打开InstallESD.dmg,点转换格式, 选择iso, 然后用 ...
- 只响应ccTouchBegan的问题
在Touch事件中,ccTouchBegan有一个返回值,而这一个返回值则决定了是否会继续响应ccTouchMoved,ccTouchEnded. 如果没有返回true的话,则直接会结束此Touch事 ...
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- NPOI心得
一个Excel文件表示为一个IWookbook,Sheet是ISheet,其它细分为IRow,ICell. 2003和2007版本为IWookbook接口的不同实现:HSSFWookbook和XSSF ...
- Asp.Net--上传大文件(页面超时)
几个方法: 修改文件上传大小的限制 以文件形式保存到服务器 转换成二进制字节流保存到数据库 将二进制通过循环的方式写入磁盘 一.修改文件上传大小的限制 通过对web.config和machine.co ...
- ArrayList 练习
ArrayList list = new ArrayList(); Random rd = new Random(); ; i <; i++) { , ); //是否包含当前数字 if (!li ...
- DOM 添加 / 更新 / 删除 XML (CURD)
获得Document /** * 获取文档 * 1.获得实例工厂 * 2.获得解析器 * 3.获得document */ 添加结点 /** * 1.获得 ...
- 一个简单C程序的汇编代码分析
几个重要的寄存器 eip - 用于存放当前所执行的指令地址 esp - 栈(顶)指针寄存器 ebp - 基址(栈底)指针寄存器 简单的C程序 int g(int x) { ; } int f(int ...
- RxJava 与 Retrofit 结合的最佳实践
转自:http://gank.io/post/56e80c2c677659311bed9841?from=timeline&isappinstalled=0&nsukey=g1D1Y6 ...
- 消息机制2 - Windows程序设计(SDK)005
消息机制2 让编程改变世界 Change the world by program 内容节选: 关于消息机制,还有三点需要补充: 消息队列是FIFO的形式 WM_PAINT,WM_TIMER 和 WM ...