zoukankan      html  css  js  c++  java
  • 嵌入式jetty

    一.maven依赖

    1. pom配置

      <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-server</artifactId>
          <version>9.2.11.v20150529</version>
      </dependency>
      <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-webapp</artifactId>
          <version>9.2.11.v20150529</version>
      </dependency>
      <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-annotations</artifactId>
          <version>9.2.11.v20150529</version>
      </dependency>
      <!-- Jetty Dependencies end here -->
      <!--Jetty Apache JSP dependency  -->
      <dependency>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>apache-jsp</artifactId>
          <version>9.2.11.v20150529</version>
      </dependency>
      <!-- JSTL Dependency -->
      <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>
      

    二. jetty加载spring,并在jetty外面获取jetty创建的spring容器

    1. web.xml配置spring监听器
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
      		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      		 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
      	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      
      	<welcome-file-list>
      		<welcome-file>index.html</welcome-file>
      	</welcome-file-list>
      
      	<context-param>
      		<param-name>contextConfigLocation</param-name>
      		<param-value>classpath*:spring-context.xml</param-value>
      	</context-param>
      
      	<!--Spring的ApplicationContext 载入 -->
      	<listener>
      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      	</listener>
      
          <!--jerssy servlet-->
      	<servlet>
      		<servlet-name>Jersey Web Application</servlet-name>
      		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      		<init-param>
      			<param-name>jersey.config.server.provider.packages</param-name>
      			<param-value>com.example</param-value>
      		</init-param>
      		<load-on-startup>1</load-on-startup>
      	</servlet>
      	<servlet-mapping>
      		<servlet-name>Jersey Web Application</servlet-name>
      		<url-pattern>/jerssyTest/*</url-pattern>
      	</servlet-mapping>
      </web-app>
      
    2. spring-contxt.xml注入带ApplicationContext的bean
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
      	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
             default-lazy-init="true">
      
      
          <!--web容器中注入applicatiobContext,使得获取的ctx使web容器中的ctx-->
          <bean id="springcontextHolder" class="server.com.SpringContextHolder" lazy-init="false"/>
      </beans>
      
    3. SpringContextHolder类
      public class SpringContextHolder implements ApplicationContextAware{
      
          private static ApplicationContext applicationContext;
          public static ApplicationContext getApplicationContext() {
              return applicationContext;
          }
      
          public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
              SpringContextHolder.applicationContext = applicationContext;
          }
      }
      

    三. 启动jetty

    1. startup类
      public class Startup {
          public static void main(String[] args) throws Exception {
              Server server = new Server(8080);
      
              WebAppContext context = new WebAppContext();
              context.setContextPath("/test");
              ClassLoader loader = Thread.currentThread().getContextClassLoader();
              context.setDescriptor(loader.getResource("webroot/WEB-INF/web.xml").getPath());
              context.setResourceBase(loader.getResource("webroot").getPath());
              context.setParentLoaderPriority(true);
              server.setHandler(context);
      
              server.start();
      
              //获取jetty创建的spring容器
              ApplicationContext ctx = SpringContextHolder.getApplicationContext();
              System.out.println(ctx);
          }
      }
      
      
  • 相关阅读:
    cd 好吃的 收藏
    2011 无代码无意义…test 指针 v1
    转 云中漫步的 电子书 from simon
    2011无代码无意义 test_gets_scanf连用 等
    svn—relocate 的原因
    转 CString,string,char*的综合比较
    2011 无代码无意义 test_内存之 变量的边界 (图)
    转 解决"应用程序配置不正确,程序无法启动"
    转 删除已存在的SVN账户信息
    C#中IO类FileInfo和Directory操作实例
  • 原文地址:https://www.cnblogs.com/72808ljup/p/5569084.html
Copyright © 2011-2022 走看看