在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(一)
来源:互联网 发布:cross over for mac 编辑:程序博客网 时间:2024/06/08 14:43
Jetty 的简单灵活特性使之很适合用于嵌入到应用程序当中。
比如开发一个多媒体资料管理库应用程序,你想用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;@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {this.applicationContext = applicationContext;}@Overridepublic 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(); }
0 0
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(一)
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(二)
- 在web应用程序中使用Spring
- 嵌入式Linux中让应用程序自动启动(转载)
- 嵌入式Linux中让应用程序自动启动(转载)
- 嵌入式Linux中让应用程序自动启动
- 嵌入式Linux中让应用程序自动启动
- 嵌入式Linux中让应用程序自动启动
- 嵌入式Linux中让应用程序自动启动!
- 嵌入式Linux中让应用程序自动启动
- 使用Gradle在嵌入式Web容器Jetty中运行Web应用
- Spring的MVC和Struts在同一个Context下使用
- Eclipse中使用jetty启动Web项目
- 嵌入式linux:嵌入式Linux中让应用程序自动启动
- 使用嵌入式jetty在Eclipse中部署struts2示例程序struts2-blank
- jetty 嵌入式web服务器使用
- Jetty学习【一】 Jetty的嵌入式使用
- main方法启动jetty java嵌入式web服务器jetty的使用
- 重定向浅淡
- Android--去除EditText边框,添加下划线,
- 【ios 汇总 - UI 视图布局 】
- 文字向上无缝滚动
- uva 1467 - Installations( 贪心+暴力)
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(一)
- java中面向对象的思想
- Android多线程之AnsyncTask
- 在应用程序中使用Spring启动嵌入式Jetty并让Web程序共享同一个Application Context(二)
- 年轻有梦,勇敢去追——我眼中的软件行业趋势
- Eclipse for php + Xdebug搭建PHP的调试环境
- iwconfig 命令
- 【MLF Week 7 上】Overfitting和Regularization
- bash中的操作