一.maven依赖
-
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容器
- 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> - 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> - 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
- 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);
}
}