码迷,mamicode.com
首页 > 其他好文 > 详细

嵌入式embeddedjetty练习

时间:2015-01-07 09:27:37      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:embeddedjetty   嵌入式embeddedjetty   嵌入式jetty   

package com.doctor.embeddedjetty;

import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

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

/**
 * 标准spring 配置(java config) 嵌入式jetty9启动,支持jsp试图,测试用例看
 * {@link ContentNegotiatingViewResolverPractice} ContentNegotiatingViewResolverPractice
 * @author doctor
 *
 * @since 2015年1月6日 下午10:40:16
 */
public class EmbeddedJettyServer3 {
	private int port;
	private String resourceBase;
	private Class<?> springRootConfiguration = null;
	private Class<?> springMvcConfiguration = null;

	private Server server;

	public EmbeddedJettyServer3(String resourceBase, Class<?> springRootConfiguration, Class<?> springMvcConfiguration) {
		this(8080, resourceBase, springRootConfiguration, springMvcConfiguration);
	}

	/**
	 * 
	 * @param port
	 * @param resourceBase 注:这里是相对路径,web src/test/resources路径,绝对路径没判断
	 * @param springRootConfiguration
	 * @param springMvcConfiguration
	 */
	public EmbeddedJettyServer3(int port, String resourceBase, Class<?> springRootConfiguration, Class<?> springMvcConfiguration) {
		this.port = port;
		this.resourceBase = resourceBase;
		this.springRootConfiguration = springRootConfiguration;
		this.springMvcConfiguration = springMvcConfiguration;
		init();
	}

	public void init() {
		server = new Server(port);
		WebAppContext context = new WebAppContext();
		context.setContextPath("/");
		try {
			context.setResourceBase(this.getClass().getResource(resourceBase).toURI().toASCIIString());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		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), "/");//处理jsp在WEB-INF目录下
//		context.addServlet(new ServletHolder(new JspServlet()), "*.jsp");
	}

	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();
		}
	}
}
测试代码
见:<a target=_blank href="https://github.com/doctorwho1986/doctor/blob/master/springmvc-practice/src/test/java/com/doctor/springframework/web/view/ContentNegotiatingViewResolverPractice.java">https://github.com/doctorwho1986/doctor/blob/master/springmvc-practice/src/test/java/com/doctor/springframework/web/view/ContentNegotiatingViewResolverPractice.java</a>


嵌入式embeddedjetty练习

标签:embeddedjetty   嵌入式embeddedjetty   嵌入式jetty   

原文地址:http://blog.csdn.net/doctor_who2004/article/details/42473245

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!