标签:http io ar os sp java on 2014 art
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(); } }
.嵌入式jetty启动spring(java配置方式),junit测试用.标准spring 配置(java config) 嵌入式jetty9启动
标签:http io ar os sp java on 2014 art
原文地址:http://blog.csdn.net/doctor_who2004/article/details/41809905