一.maven依赖
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容器
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/* spring-contxt.xml注入带ApplicationContext的bean
```xml <?xml version="1.0" encoding="UTF-8"?>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
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); }
}
```