使用Spring启动内嵌Jetty的2种方式

在应用程序内使用Spring启动内嵌的Jetty服务总共有2种方式:

1. 一种是启动一个独立运行的Jetty服务,使用独立的Spring配置,跟应用之间完全独立

2. 一种是启动一个与应用共享ApplicationContext的服务,可以访问应用的全部内容

 

对于第一种独立的Jetty服务:

 

        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector(); 
        connector.setPort(12345); 
        server.addConnector(connector); 
       

        final ServletHolder servletHolder = new ServletHolder(new CXFServlet());
        final ServletContextHandler context = new ServletContextHandler();
        context.setContextPath("/data-service");
        context.addServlet(servletHolder, "/*");

        context.addEventListener(new ContextLoaderListener());
        context.setInitParameter("contextConfigLocation", "classpath:spring-server.xml");
        
        //设置字符编码,否则默认是使用机器的编码,一般是ISO-8859
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
	filter.setEncoding("UTF-8");
	filter.setForceEncoding(true);
	context.addFilter(new FilterHolder(filter), "/*", EnumSet.allOf(DispatcherType.class));
         
        server.setHandler(contextHandler);
        server.start();
        server.join();

 

对于第二种方式,在构造ContextHandler时,稍微有所不同,同时需要注入当前应用的ApplicationContext

public class EmbedDataServiceServer implements ApplicationContextAware {
   private ApplicationContext applicationContext;
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
        this.applicationContext = applicationContext;  
    }

   public void start() throws Exception {
        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector(); 
        connector.setPort(12345); 
        server.addConnector(connector); 
       

        final WebAppContext context = new WebAppContext();
	context.setContextPath("/" + config.getCallbackContextPath());
	context.setConfigurationDiscovered(true);
	context.setParentLoaderPriority(true);
	context.setResourceBase("web");
        context.addServlet(CXFServlet.class, "/*");

	context.setClassLoader(applicationContext.getClassLoader());	
	XmlWebApplicationContext xmlWebAppContext = new XmlWebApplicationContext();
	xmlWebAppContext.setParent(applicationContext);
	xmlWebAppContext.setConfigLocation("");
	xmlWebAppContext.setServletContext(context.getServletContext());
	xmlWebAppContext.refresh();
		
	context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlWebAppContext);
        
        //设置字符编码,否则默认是使用机器的编码,一般是ISO-8859
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
	filter.setEncoding("UTF-8");
	filter.setForceEncoding(true);
	context.addFilter(new FilterHolder(filter), "/*", EnumSet.allOf(DispatcherType.class));
         
        server.setHandler(contextHandler);
        server.start();
        server.join();
   }
}

 

Java版本: JDK 1.7

使用jar包版本:

spring 4.1.4.RELEASE

cxf 2.7.6

jetty  8.1.17.v20150415

 

这里参考了另外一位老兄的记录整理而来,参考地址:

http://blog.csdn.net/danieluk/article/details/18750115

你可能感兴趣的:(Spring,CXF)