博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
嵌入式jetty
阅读量:4687 次
发布时间:2019-06-09

本文共 2756 字,大约阅读时间需要 9 分钟。

一.maven依赖

  1. pom配置

    org.eclipse.jetty
    jetty-server
    9.2.11.v20150529
    org.eclipse.jetty
    jetty-webapp
    9.2.11.v20150529
    org.eclipse.jetty
    jetty-annotations
    9.2.11.v20150529
    org.eclipse.jetty
    apache-jsp
    9.2.11.v20150529
    jstl
    jstl
    1.2

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

  1. web.xml配置spring监听器

    ```xml
    <?xml version="1.0" encoding="UTF-8"?>

    index.html
    contextConfigLocation
    classpath*:spring-context.xml
    org.springframework.web.context.ContextLoaderListener
    Jersey Web Application
    org.glassfish.jersey.servlet.ServletContainer
    jersey.config.server.provider.packages
    com.example
    1
    Jersey Web Application
    /jerssyTest/*
    ```
  2. spring-contxt.xml注入带ApplicationContext的bean

    ```xml
    <?xml version="1.0" encoding="UTF-8"?>

    ```
  3. SpringContextHolder类

    ```java
    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类

    ```java
    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); }

    }

    ```

转载于:https://www.cnblogs.com/72808ljup/p/5569084.html

你可能感兴趣的文章