Position:Home > Jetty实战之 嵌入式Jetty集成Spring运行

Jetty实战之 嵌入式Jetty集成Spring运行

本文链接:http://blog.csdn.net/kongxx/article/details/7227107

1. 首先修改pom.xml文件,添加spring的依赖项

[html]   view plain copy print ?
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.google.code.garbagecan.jettystudy</groupId>  
  5.     <artifactId>jettystudy</artifactId>  
  6.     <packaging>jar</packaging>  
  7.     <version>1.0-SNAPSHOT</version>  
  8.     <name>jettystudy</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <build>  
  11.         <plugins>  
  12.             <plugin>  
  13.                 <artifactId>maven-compiler-plugin</artifactId>  
  14.                 <inherited>true</inherited>  
  15.                 <configuration>  
  16.                     <source>1.6</source>  
  17.                     <target>1.6</target>  
  18.                     <debug>true</debug>  
  19.                 </configuration>  
  20.             </plugin>  
  21.         </plugins>  
  22.     </build>  
  23.     <dependencies>  
  24.         <dependency>  
  25.             <groupId>org.eclipse.jetty.aggregate</groupId>  
  26.             <artifactId>jetty-all</artifactId>  
  27.             <version>8.0.4.v20111024</version>  
  28.             <type>jar</type>  
  29.             <scope>provided</scope>  
  30.         </dependency>  
  31.           
  32.         <dependency>  
  33.             <groupId>org.springframework</groupId>  
  34.             <artifactId>spring</artifactId>  
  35.             <version>2.5.6</version>  
  36.             <type>jar</type>  
  37.             <scope>provided</scope>  
  38.         </dependency>  
  39.           
  40.         <dependency>  
  41.             <groupId>junit</groupId>  
  42.             <artifactId>junit</artifactId>  
  43.             <version>3.8.1</version>  
  44.             <scope>test</scope>  
  45.         </dependency>  
  46.     </dependencies>  
  47. </project>  
2. 创建一个Server类,用来通过spring来启动Jetty server [java]   view plain copy print ?
  1. package com.google.code.garbagecan.jettystudy.sample4;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class MyServer {  
  6.     public static void main(String[] args) throws Exception {  
  7.         new ClassPathXmlApplicationContext("/com/google/code/garbagecan/jettystudy/sample4/spring.xml");  
  8.     }  
  9. }  
3. 创建一个Handler类,用了处理http请求 [java]   view plain copy print ?
  1. package com.google.code.garbagecan.jettystudy.sample4;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.eclipse.jetty.server.Request;  
  10. import org.eclipse.jetty.server.handler.AbstractHandler;  
  11.   
  12. public class MyHandler extends AbstractHandler {  
  13.     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)  
  14.             throws IOException, ServletException {  
  15.         response.setContentType("text/html;charset=utf-8");  
  16.         response.setStatus(HttpServletResponse.SC_OK);  
  17.         baseRequest.setHandled(true);  
  18.         response.getWriter().println("<h1>Hello World</h1>");  
  19.         response.getWriter().println("<li>Request url: " + target + "</li>");  
  20.         response.getWriter().println("<li>Server port: " + request.getServerPort() + "</li>");  
  21.     }  
  22. }  
4. 创建一个spring配置文件,并放在com/google/code/garbagecan/jettystudy/sample4/spring.xml位置,内容如下.
[java]   view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.   
  6.     <bean id="Server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">  
  7.         <property name="connectors">  
  8.             <list>  
  9.                 <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">  
  10.                     <property name="port" value="8080" />  
  11.                 </bean>  
  12.             </list>  
  13.         </property>  
  14.   
  15.         <property name="handler">  
  16.             <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerList">  
  17.                 <property name="handlers">  
  18.                     <list>  
  19.                         <bean class="com.google.code.garbagecan.jettystudy.sample4.MyHandler" />  
  20.                         <bean class="org.eclipse.jetty.server.handler.DefaultHandler" />  
  21.                     </list>  
  22.                 </property>  
  23.             </bean>  
  24.         </property>  
  25.     </bean>  
  26. </beans>  
其中定义了Jetty Server的配置,包括Connector和Handler等等。
5. 运行MyServer类,然后通过http://localhost:8080/来访问。
Related
Your Answer
(Ctrl+Enter To Post)   
    Hotest
    Copyright © 2015-2017 163JAVA.COM All Rights Reserved.