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 ...
随机推荐
- Java JDBC连接SQL Server2005错误:通过port 1433 连接到主机 localhost 的 TCP/IP 连接失败
错误原因例如以下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cann ...
- HID Keyboard & Mouse descriptor.
在USB中,USB HOST是通过各种描述符来识别设备的,有设备描述符,配置描述符,接口描述符,端点描述符,字符串描述符,报告描述符等等.USB报告描述符(Report Descriptor)是HID ...
- jquery之多重判断
var appPath = getAppPath(); $(function(){ $('#addTeskDlg').window('close'); teskGrid(); }); function ...
- Slider( 滑动条) 组件
本节课重点了解 EasyUI 中 Slider(滑动条)组件的使用方法,这个组件依赖于Draggable(拖动)组件. 一. 加载方式//class 加载方式<input class=" ...
- IE9的window.showmodaldialog显示问题
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...
- bootstrap-datetimepicker使用记录
版本:V2.0 1.bootstrap-datetimepicker.min.css 2.bootstrap-datetimepicker.min.js 3.bootstrap-datetimepic ...
- 求解:C#.Net 远程方法调用失败 (Exception from HRESULT: 0x800706BE)
服务器:Windows Server2003 sp2服务器 客户端:XP SP3 内容:C#Winform客户端调用服务器的Excel模板生成报表的时候,生成失败,抛出的异常如下: TargetInv ...
- 命令提示符CMD远程连接Mysql学习笔记
我想要用Cmd可以像SecureCRT一样远程连接数据库,查询数据,因为用cmd的话可以用批处理,方便脚本调用 第一步:直接使用命令 mysql –h ip –u user –p,本地运行了该命令提示 ...
- 关于uC/OS的简单学习(转)
1.微内核 与Linux的首要区别是,它是一个微内核,内核所实现的功能非常简单,主要包括: 一些通用函数,如TaskCreate(),OSMutexPend(),OSQPost()等. 中断处理函数, ...
- const volatile int i
问题: const volatile int i=10:这行代码有没有问题?如果没有,那 i 到底是什么属性? 回答: 没有问题,例如只读的状态寄存器.它是volatile,因为它可能被意想不到地改变 ...