.嵌入式jetty启动spring(java配置方式),junit测试用.标准spring 配置(java config) 嵌入式jetty9启动
2014-12-08 21:37
519 查看
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测试用
- Spring4 javaConfig配置方式并集成JUnit
- spring普通java工程的配置启动方式
- spring-batch 基于java config 配置方式
- Java Spring搭配Junit测试配置
- Spring JavaConfig方式配置模板视图
- Spring3.2 中 Bean 定义之基于 Annotation 和 Java Code 配置方式的源码解析
- web.xml 通过contextConfigLocation配置spring 的方式
- Spring 中 MongDB java Bean 的映射配置(注解方式)
- java.lang.OutOfMemoryError: PermGen space修改启动配置的解决方式
- 适应各种开发,测试,线上,线下环境的Spring配置方式
- Spring和junit测试之配置文件路径
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(一)
- spring AOP 配置事务不起作用的问题(用Junit测试service)
- junit测试用例加载spring配置文件
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- spring中 使用JUnit4 测试 代码[模拟服务器启动]
- Spring Bean配置方式之一:Java配置
- spring 配置junit 启动时出现 ClassNotFound警告
- spring dbunit junit hibernate 注解方式测试集合