熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

嵌入式Jetty集成Spring運行

2022-06-13   來源: Java核心技術 

   首先修改pomxml文件添加spring的依賴項[html] view plaincopy <project xmlns= xmlnsxsi=instance xsischemaLocation= v__xsd> <modelVersion></modelVersion> <groupId>degarbagecanjettystudy</groupId> <artifactId>jettystudy</artifactId> <packaging>jar</packaging> <version>SNAPSHOT</version> <name>jettystudy</name> <url>;/url> <build> <plugins> <plugin> <artifactId>mavencompilerplugin</artifactId> <inherited>true</inherited> <configuration> <source></source> <target></target> <debug>true</debug> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>orgeclipsejettyaggregate</groupId> <artifactId>jettyall</artifactId> <version>v</version> <type>jar</type> <scope>provided</scope> </dependency>

  <dependency> <groupId>orgspringframework</groupId> <artifactId>spring</artifactId> <version></version> <type>jar</type> <scope>provided</scope> </dependency>

  <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version></version> <scope>test</scope> </dependency> </dependencies> </project> 創建一個Server類用來通過spring來啟動Jetty server [java] view plaincopy package degarbagecanjettystudysample

  import orgntextsupportClassPathXmlApplicationContext

  public class MyServer { public static void main(String[] args) throws Exception { new ClassPathXmlApplicationContext(/com/google/code/garbagecan/jettystudy/sample/springxml} 創建一個Handler類用了處理http請求[java] view plaincopy package degarbagecanjettystudysample

  import javaioIOException

  import javaxservletServletExceptionimport javaxservlethttpHttpServletRequestimport javaxservlethttpHttpServletResponse

  import orgeclipsejettyserverRequestimport orgeclipsejettyserverhandlerAbstractHandler

  public class MyHandler extends AbstractHandler { public void handle(String target Request baseRequest HttpServletRequest request HttpServletResponse response)

  throws IOException ServletException { responsesetContentType(text/htmlcharset=utfresponsesetStatus(HttpServletResponseSC_OK)baseRequestsetHandled(true)responsegetWriter()println(<h>Hello World</h>responsegetWriter()println(<li>Request url + target + </li>responsegetWriter()println(<li>Server port + requestgetServerPort() + </li>} 創建一個spring配置文件並放在com/google/code/garbagecan/jettystudy/sample/springxml位置內容如下

  [java] view plaincopy <?xml version= encoding=UTF?> <beans xmlns= xmlnsxsi=instance xsischemaLocation= beansxsd>

  <bean id=Server class=orgeclipsejettyserverServer initmethod=start destroymethod=stop> <property name=connectors> <list> <bean id=Connector class=orgeclipsejettyservernioSelectChannelConnector> <property name=port value= /> </bean> </list> </property>

  <property name=handler> <bean id=handlers class=orgeclipsejettyserverhandlerHandlerList> <property name=handlers> <list> <bean class=degarbagecanjettystudysampleMyHandler /> <bean class=orgeclipsejettyserverhandlerDefaultHandler /> </list> </property> </bean> </property> </bean> </beans>其中定義了Jetty Server的配置包括Connector和Handler等等

   運行MyServer類然後通//localhost/來訪問


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26882.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.