在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(一)
2014-01-25 00:45
501 查看
Jetty 的简单灵活特性使之很适合用于嵌入到应用程序当中。
比如开发一个多媒体资料管理库应用程序,你想用Web完成所有的用户界面,这时就可以让Jetty嵌入到你的应用程序作为Web容器。
在应用程序里启动 Jetty 的方法很简单,使用如下代码即可:
然后编写一个用于启动 Jetty 的类
要注意这个类必须实现 ApplicationContextAware 接口,以让 spring 把 application context 设置进来,同时从上面的代码可以看到其实我们自己创建了 XmlWebApplicationContext 并把它传入 webAppContext 的 attribute 集合。
要主要这个类不要 new 并且执行,而是让 applicationContext 配置它并且执行它。
applicationContext.xml 的部分配置代码如下:
现在应用程序的 main() 方法里面只需加载 Spring context 即可,其他的所有工作就让 Spring 来完成吧。
比如开发一个多媒体资料管理库应用程序,你想用Web完成所有的用户界面,这时就可以让Jetty嵌入到你的应用程序作为Web容器。
在应用程序里启动 Jetty 的方法很简单,使用如下代码即可:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
然后编写一个用于启动 Jetty 的类
package com.test; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.webapp.WebAppContext; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; /* To embed a Jetty server, the following steps are typical: 1. Create the server 2. Add/Configure Connectors 3. Add/Configure Handlers 4. Add/Configure Servlets/Webapps to Handlers 5. start the server 6. wait (join the server to prevent main exiting). */ public class JettyDaemon implements ApplicationContextAware{ private Server server; private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } @Override public void start() throws Exception{ server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(8080); server.addConnector(connector); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/"); webAppContext.setDescriptor("web/WEB-INF/web.xml"); webAppContext.setResourceBase("web"); webAppContext.setConfigurationDiscovered(true); webAppContext.setParentLoaderPriority(true); server.setHandler(webAppContext); // 以下代码是关键 webAppContext.setClassLoader(applicationContext.getClassLoader()); XmlWebApplicationContext xmlWebAppContext = new XmlWebApplicationContext(); xmlWebAppContext.setParent(applicationContext); xmlWebAppContext.setConfigLocation(""); xmlWebAppContext.setServletContext(webAppContext.getServletContext()); xmlWebAppContext.refresh(); webAppContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlWebAppContext); server.start(); } }
要注意这个类必须实现 ApplicationContextAware 接口,以让 spring 把 application context 设置进来,同时从上面的代码可以看到其实我们自己创建了 XmlWebApplicationContext 并把它传入 webAppContext 的 attribute 集合。
要主要这个类不要 new 并且执行,而是让 applicationContext 配置它并且执行它。
applicationContext.xml 的部分配置代码如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" … … … … … …> <!-- daemons --> <bean id="webDaemon" class="com.test.JettyDaemon" init-method="start" /> … … … … … …
现在应用程序的 main() 方法里面只需加载 Spring context 即可,其他的所有工作就让 Spring 来完成吧。
public static void main(String[] args){ applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); applicationContext.registerShutdownHook(); }
相关文章推荐
- Android--去除EditText边框,添加下划线,
- xmapp中 使用admin的权限打开mysql时出现错误1045
- 03 Unity3d轻松做汽展、展厅--《程序员学Unity3d》
- 手机平板等移动端适配跳转URL的js代码
- [Android] Android开发优化之——从代码角度进行优化
- [Android] Android开发优化之——使用软引用和弱引用
- Android] AndroidManifest.xml文件解析
- Android中使用自定义的字体
- android小项目之音乐播放器二
- android小项目之音乐播放器一
- Android 依赖注入:Dagger 实例讲解(Demo下载)
- android小项目之新闻客户端一
- ThinkPad X230I 预装Win8换其它系统前的BIOS设置
- android小项目之新闻客户端五
- Android中关于线性布局基线对齐的讲述
- android小项目之新闻客户端四
- android小项目之新闻客户端三
- android小项目之新闻客户端二
- android小项目之新闻客户端一
- android功能模块之通讯模块七