java

位置:IT落伍者 >> java >> 浏览文章

嵌入式Jetty集成Spring运行


发布日期:2019年12月17日
 
嵌入式Jetty集成Spring运行

首先修改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/来访问

               

上一篇:java中如何把图片转换成二进制流

下一篇:Java模拟ping功能