web项目Spring框架下虚拟测试端编写
来源:互联网 发布:mac系统dmg下载地址 编辑:程序博客网 时间:2024/06/15 19:24
后台模拟测试
现行web开发使用mvc分层模式下往往页面,与底层分开来进行开发,那么如何在没有页面的情况下进行测试哪?
首先要明确一点 Spring框架下的类属性的初始化,是在程序加载.xml文件中进行的。
public class RunInfoModuleHandle {private static Log log = LogFactory.getLog(RunInfoModuleHandle.class);private BiztxlogDAO biztxlogDAO;private DailyInfoListDAO dailyInfoListDAO;private ReportRemedyDAO reportRemedyDAO;private PosBiztxlogDAO posBiztxlogDAO;private PosBizDailyDAO PosBizDailyDAO;private PosReportRemedyDAO public ReportRemedyDAO getReportRemedyDAO() {return reportRemedyDAO;}public void setReportRemedyDAO(ReportRemedyDAO reportRemedyDAO) {this.reportRemedyDAO = reportRemedyDAO;}
新建一个对象,一般 ReportRemedyDAO reportRemedyDAO = new ReportRemedyDAO();
不过在spring框架下,根据配置文件
<bean id="runInfoModuleHandle"
class="com.zjft.xconsole.common.tcp.RunInfoModuleHandle">
<property name="biztxlogDAO" ref="BiztxlogDAO"></property>
<property name="dailyInfoListDAO" ref="DailyInfoListDAO"></property>
<property name="reportRemedyDAO" ref="ReportRemedyDAO"></property>
<property name="posBiztxlogDAO" ref="PosBiztxlogDAO"></property>
<property name="posBizDailyDAO" ref="PosBizDailyDAO"></property>
<property name="posReportRemedyDAO" ref="PosReportRemedyDAO"></property>
</bean>
完成了new 的功能。
那么回到刚刚的问题,分层开发中如何进行测试。很多同学会说main方法就可了,没错,不过读过刚刚的初始化问题,相信大家应该明白,不光有main就可以了。
下面为大家介绍2个类的使用:ClassPathXmlApplicationContext和FileSystemXmlApplicationContext:获得Spring中定义的Bean实例(对象).
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");
如果是两个以上:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});
或者用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
注意:ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件],
FileSystemXmlApplicationContext可以取到任意路径下文件,不过需要配置路径。
对于FileSystemXmlApplicationContext:
默认表示的是两种:
1.没有盘符的是项目工作路径,即项目的根目录;
2.有盘符表示的是文件绝对路径.
如果要使用classpath路径,需要前缀classpath:
spring中对bean的管理有两种类型 ApplicationContext和BeanFactory
ApplicationContext在容器启动时候会全部初始化bean,而factory只会在调用get(“abc”)时候才会初始化
那么我们就可以用main函数来测试了。
import javax.faces.application.Application;import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zjft.xconsole.common.tcp.RunInfoModuleHandle;public class KoalaServerStart { /** * @param args */public static void main(String[] args) {try {System.out.println("application begin start, please wait......");BeanFactory appContext = new FileSystemXmlApplicationContext("WEB-INF/data.xml");RunInfoModuleHandle app = (RunInfoModuleHandle) appContext.getBean("runInfoModuleHandle");app.impDailyInfo();} catch (Exception e) {e.printStackTrace();}}}
使用以上的代码,那么就可以实现我们要做的事了。
- web项目Spring框架下虚拟测试端编写
- SSM框架下编写java web项目
- Spring框架启动--Web项目
- web.xml详细解读(flex项目,框架:cairngorm--blazeDS--spring--ibatis)(正在编写中)
- Spring框架下Junit测试
- Spring框架使用内幕之IOC&Bean的生命周期&整合Web项目和Junit测试
- 使用selenium进行web测试项目框架
- web开发-MyEclipse下maven创建Web项目Spring+Spring MVC+MyBatis框架集成-学习笔记十六
- 实战-web项目集成spring框架
- mac下IDEA配置Spring框架+mybatis+maven配置管理的web项目过程详解
- Tomcat下配置虚拟路径管理web项目
- Spring Web MVC框架(十一) Spring Web MVC测试框架
- servlet3.0下构建spring web项目
- 编写Web框架
- spring+ibatis框架下事务代码的编写示例
- Spring框架下编写事物回滚小记
- 怎样用Ant测试Spring项目(Ant测试自动化--下)
- 理解Web项目虚拟路径
- GlusterFS:No such file or directory
- struts的bean标签
- ruby&python 从CGI 到 WSGI 到 Rack, 顺带Sinatra
- Linux kernel --- VirtualBox
- 专家讲解:Md5(base64)加密与解密实战
- web项目Spring框架下虚拟测试端编写
- 多个文件下使用全局变量
- lr_convert_string_encoding
- 用Mac和Linux命令行工具使用Windows Azure
- 二十六、JCreator使用初步
- IIS 使用域账户访问SQL 需要配置
- java封装
- 面向对象的原则
- mysql之insert