.嵌入式jetty启动spring(java配置方式),junit测试用.标准spring 配置(java config) 嵌入式jetty9启动

package com.doctor.embeddedjetty;

import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * 标准spring 配置(java config) 嵌入式jetty9启动
 * @author doctor
 *
 * @time   2014年12月8日 下午4:07:40
 */
public class EmbeddedJettyServer2 {
	private int port ;
	private Class<?> springRootConfiguration = null;
	private Class<?> springMvcConfiguration = null;

	private Server server;

	public EmbeddedJettyServer2(Class<?> springRootConfiguration,Class<?> springMvcConfiguration){
		this(8080, springRootConfiguration,springMvcConfiguration);
	}
	public EmbeddedJettyServer2(int port,Class<?> springRootConfiguration,Class<?> springMvcConfiguration){
		this.port = port;
		this.springRootConfiguration = springRootConfiguration;
		this.springMvcConfiguration = springMvcConfiguration;
		init();
	}

	public void init(){
		server = new Server(port);
		ServletContextHandler context = new ServletContextHandler();
		context.setContextPath("/");
		server.setHandler(context);

		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(this.springRootConfiguration);
		context.addEventListener(new ContextLoaderListener(rootContext));

		AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
		mvcContext.register(springMvcConfiguration);
		DispatcherServlet dispatcherServlet = new DispatcherServlet(mvcContext);
		context.addServlet(new ServletHolder(dispatcherServlet), "/*");

	}
	public void start() throws Exception{
		if (server!= null) {
			if (server.isStarting() || server.isStarted() || server.isRunning() ) {
				return;
			}
		}
		TimeUnit.SECONDS.sleep(3);
		server.start();
	}

	public void stop() throws Exception{
		if (server != null) {
			if (server.isRunning()) {
				server.stop();
			}
		}
	}

	public void join() throws InterruptedException{
		if (server!=null) {
			server.join();
		}
	}
}

测试用例:

package com.doctor.embeddedjetty;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;

import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.junit.Test;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 标准spring 配置(java config) 嵌入式jetty9启动 测试
 *
 * @author doctor
 *
 * @time   2014年12月8日 下午4:06:41
 */
public class EmbeddedJettyServer2Test {

	@Test
	public void test() throws Throwable{
		EmbeddedJettyServer2 embeddedJettyServer = new EmbeddedJettyServer2(SpringRootConfiguration.class, SpringMvcConfiguration.class);
		embeddedJettyServer.start();

		Response response = Request.Get("http://localhost:8080/embeddedJettyServer2Test").execute();
		assertThat(response.returnContent().asString(), equalTo("SimpleServiceImpl"));
		embeddedJettyServer.stop();
	}

	@Configuration
	@ComponentScan("com.doctor.embeddedjetty")
	public static class SpringRootConfiguration{

	}

	@Configuration
	@ComponentScan("com.doctor.embeddedjetty")
	public static class SpringMvcConfiguration{

	}

}
package com.doctor.embeddedjetty;

public interface SimpleService {
	public String getMessage();
}
package com.doctor.embeddedjetty;

import org.springframework.stereotype.Component;

@Component("simpleService")
public class SimpleServiceImpl implements SimpleService {

	@Override
	public String getMessage() {
		return "SimpleServiceImpl";
	}

}
package com.doctor.embeddedjetty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SImpleController2 {

	@Autowired
	@Qualifier("simpleService")
	private SimpleService simpleService;

	@RequestMapping(value="/embeddedJettyServer2Test",method=RequestMethod.GET)
	@ResponseBody
	public String getMessage(){
		return simpleService.getMessage();
	}
}
时间: 2024-08-01 10:45:04

.嵌入式jetty启动spring(java配置方式),junit测试用.标准spring 配置(java config) 嵌入式jetty9启动的相关文章

Java进阶 五 Junit测试

我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在程序中只用到该函数的一小部分功能,并且经过调试可以确定,这一小部分功能是正确的.但是,我们同时应该确保每一个函数都完全正确,因为如果我们今后如果对程序进行扩展,用到了某个函数的其他功能,而这个功能有bug的话,那绝对是一件非常郁闷的事情.所以说,每编写完一个函数之后,都应该对这个函数的方方面面进行测试,这样的测试我们称之为单元测试.传统的编程方式,进行单元测试是一件很麻烦的事情,你要重新写另外一个程序,在该程

【java】使用Junit测试线程过程中出现的小问题

本文内容介绍在使用Junit进行线程测试的时候出现的一个小问题,自己简单做一个记录,以便后续查看 在使用java编写多线程并发实验程序时在Juint写了测试程序,但并没有得到预想的效果. 直接上代码 线程类: package sm.examples.threaddemo; import org.apache.log4j.Logger; public class Thread1 implements Runnable { private static final Logger logger = L

Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML 定义的 Bean 信息转换为 Spring 框架的 Bean Definition 对象的处理过程,向读者展示了 Spring 框架的奥妙之处,可以加深开发人员对 Spring 框架的理解. 0 评论: 秦 天杰, 软件工程师, IBM China 2013 年 9 月 02 日 内容 在 IBM

【STM】GPIO引脚配置方式

配置方式: 普通 GPIO 输入:根据需要配置该引脚为浮空输入.带弱上拉输入或带弱下拉输入,同时不要使能该引脚对应的所有复用功能模块 普通 GPIO 输出:根据需要配置该引脚为推挽输出或开漏输出,同时不要使能该引脚对应的所有复用功能模块 普通模拟输入:配置该引脚为模拟输入模式,同时不要使能该引脚对应的所有复用功能模块 内置外设的输入:根据需要配置该引脚为浮空输入. 带弱上拉输入或带弱下拉输入,同时使能该引脚对应的某个复用功能模块 内置外设的输出:根据需要配置该引脚为复用推挽输出或复用开漏输出,同

fpga配置方式 .jic固化为ps模式

FPGA不同下载方式的区别[扫盲]以及如何利用AS模式固化程序(转载) 主动配置方式(AS)和被动配置方式(PS)和最常用的(JTAG)配置方式: AS由FPGA器件引导配置操作过程,它控制着外部存储器和初始化过程,EPCS系列.如EPCS1,EPCS4配置器件专供AS模式,目前只支持 Cyclone系列.使用Altera串行配置器件来完成.Cyclone期间处于主动地位,配置期间处于从属地位.配置数据通过DATA0引脚送入 FPGA.配置数据被同步在DCLK输入上,1个时钟周期传送1位数据.(

思科支持的不同板卡配置方式

这次简单和大家总结下思科所支持的不同板卡的配置方式,很显然,板卡都认识不全,或者根本不了解,碰到需要针对不同板卡类型进行配置时,我们根本不需要动手,毫无还手之力.我仍旧是大家的好朋友,现在为大家讲下常用的板卡类型: 用过思科路由器,比如C7206 .C2811.C3845等,我们都或多或少的碰到了在接对应业务配置端口方面手足不措,是的,希望你看到下面后,对这些不是问题的问题不再有陌生感.距离感! 思科支持的板卡类型还是比较多,咋的一看,容易眼花缭乱,即使乱花渐渐迷人眼,我们也要用我们一双火眼金睛

FPGA的配置方式

FPGA的配置方式有以下几种,JTAG,AS,PS,AP,FPP等几种. JTAG模式.JTAG模式下将.sof文件下载到FPGA内部的RAM内部进行运行,掉电程序丢失,主要用于前期的调试阶段.不过JTAG模式下可以下载JIC文件到配置Flash中,也可以达到固化程序的作用.JTAG有专用的PIN连接. 同时JTAG下载模式下支持3.3V,3.0V,2.5V和1.8V,1.5V的电压支持模式,不同的电压硬件电路会有不同. a)      3.3V,3.0V,2.5V的bank电压,TMS,TDI

junit测试,spring中使用

很久没用junit测试了,今天在spring框架下面使用junit测试发现怎么都不行 网上查了一些资料才发现我的项目里面少了一些jar包,现在将这个jar包信息放上去,以免以后又忘了 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope&

struts2简单入门-Action的三种配置方式

普通的配置方式 优点:可读性高 缺点:重复的配置太多. 使用情况 一个actian只有一个方法,只需要处理一种请求. 代码演示 1 <action name="voteResult" class="com.vot.action.VoteResultAction"> 2 <result name="success">VoteResult.jsp</result> 3 </action> 动态方法调用D