It took me a while, but I got a refreshable Spring web context to work! It wasn't too hard, but I was hoping it would be easier.
The biggest problem I faced was that the venerable DispatcherServlet initializes member variables based on the ApplicationContext that it creates. I created a hacky subclass to get around this problem:
public class RefreshableDispatcherServlert extends DispatcherServlet {
public static final String SERVLET_ATTRIBUTE_NAME = RefreshableDispatcherServlert.class
.getName()
+ "_SERVLET_ATTRIB";
protected HandlerExecutionChain getHandler(HttpServletRequest request, boolean cache)
throws Exception {
RequestContextHolder.getRequestAttributes().setAttribute(getClass().getName(), this,
RequestAttributes.SCOPE_REQUEST);
return super.getHandler(request, cache);
}
}
I then use that dispatcher servlet in a "refreshing" controller:
public class RefreshingController implements Controller, ApplicationContextAware {
private ApplicationContext applicationContext;
private Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
refresh(response, request, configurableApplicationContext);
return null;
}
private void refresh(HttpServletResponse response, HttpServletRequest request,
ConfigurableApplicationContext context) throws IOException, ServletException {
long start = System.currentTimeMillis();
log(response, "refreshing context");
refreshParent(response, request, context, start);
refreshServlet();
log(response, "refreshed servlet in " + (System.currentTimeMillis() - start) + " ms.");
}
private void refreshParent(HttpServletResponse response, HttpServletRequest request,
ConfigurableApplicationContext context, long start) throws IOException {
ConfigurableApplicationContext parent = (ConfigurableApplicationContext) context
.getParent();
if (parent != null && "true".equals(request.getParameter("reloadParent"))) {
parent.refresh();
log(response, "refreshed parent in " + (System.currentTimeMillis() - start) + "ms");
}
}
private void refreshServlet() throws ServletException {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
String key = RefreshableDispatcherServlert.SERVLET_ATTRIBUTE_NAME;
DispatcherServlet servlet = (DispatcherServlet) requestAttributes.getAttribute(key,
RequestAttributes.SCOPE_REQUEST);
servlet.init();
}
private void log(HttpServletResponse response, String string) throws IOException {
log(string, response.getWriter());
}
private void log(String string, PrintWriter writer) {
logger.info(string);
writer.write(string);
writer.write("");
writer.flush();
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
It's hacky but it works for my needs . I prevously would have to bounce my Web's Fear application context which takes 2-5 minutes. It also saves JSP recompilations which take quite a while in our setup... The DispatcherServlet reloading taks 10 seconds. The top level app context + the DispatcherServlet context takes 30 seconds.
We have a huge system with lots of dependencies and a beast of a development environment. I'll be thinking about modularization and speed ups, including some potentially neat Spring tricks...
来自:http://www.jroller.com/Solomon/entry/reloading_a_spring_web_application
分享到:
相关推荐
cas-addon-spring-resource-reloading-support 该项目是作为 Unicon一部分开发的。 提供此模块的专业支持/集成协助。 欲了解更多信息,。 与在 CAS 服务器的上下文中处理 Spring Framework 资源运行时重新加载有关...
安装 pip install reloading 用法要在每次迭代之前从源代码重新加载 for 循环的主体,只需使用重新加载包装迭代器,例如 from reloading import reloading for i in reloading(range(10)): # here could be your ...
* Web application reloading and static fields in shared libraries * Tomcat on Linux * Enabling SSI and CGI Support * Security manager URLs * Symlinking static resources * Viewing the Tomcat Change Log...
* Web application reloading and static fields in shared libraries * Security manager URLs * Symlinking static resources * Viewing the Tomcat Change Log * Cryptographic software notice * When all else ...
标题 "gmdiscord-web:gmdiscord-web" 暗示我们正在处理一个与 Discord 相关的 Web 项目,可能是一个用户界面或者插件,用于增强 Discord 的功能或用户体验。项目名称中的 "gmdiscord" 可能是 "GameMasterDiscord" ...
-- Spring Boot DevTools for hot reloading --> <groupId>org.springframework.boot <artifactId>spring-boot-devtools <optional>true <!-- Spring Boot AOP Starter --> <groupId>org.spring...
AJAX driven simple chat Detailed description: This class can be used to implement a simple Web based chat system that uses AJAX to update the chat dialog display area without reloading the ...
通过上述知识点的梳理,我们可以看出Struts2不仅是一个功能强大且灵活的Java Web框架,而且还能够与其他流行框架如Spring进行有效的整合。这对于希望构建复杂Web应用的开发者来说是非常有价值的。旺旺老师的教程为...
在探讨“play-asset-reloading”这一主题时,我们首先要理解的是它与游戏开发和Web应用性能优化的关系。Play Framework是一个流行的、用Scala和Java构建的开源Web应用框架,它支持快速开发,提供了丰富的功能来处理...
《Qdai Reloading-crx插件:提升在线学习体验的高效工具》 Qdai Reloading-crx插件是一款专为日本语用户设计的浏览器扩展程序,旨在优化Moodle平台的学习流程,尤其适合那些需要频繁刷新页面以获取最新内容的在线...
Unit testing is a crucial aspect of software development that ensures the reliability of individual components or functions within an application. In the context of Django, unit testing helps ...
is a technique used for creating faster and more interactive web applications by exchanging data with a server behind the scenes and updating parts of a web page, without reloading the entire page....
论文题目是《YOLOv6: A Single-Stage Object Detection Framework for Industrial Applications》。 本框架同时专注于检测的精度和推理效率,在工业界常用的尺寸模型中:YOLOv6-nano 在 COCO 上精度可达 35.0% AP,...
Listen on http://localhost:3000. "nuxt-build": "nuxt build",//官方 Build the nuxt.js web application for production. "nuxt-start": "nuxt build && nuxt start",//官方 Start ExpressJS serverNew in ...
A scripting engine allows an end user to customize an application to his or her needs without having to recompile it. In addition, you can update your applications by just sending a new script file ...
The-Ultimate-Boilerplate, web pack 2,响应 hotloader 3,响应路由器 v4,代码拆分和更多 : 反应重现和 web service的最终入门工具包React/Redux/Hot-reloading/Webpack/React-router/Express/Enzyme 通过代码拆分...
npm install --save reloading 用法 var React = require('react'); var Loading = require('reloading'); var App = React.createClass({ getInitialState: function () { return { loading: false }; }, ...
4. **热重载(Hot Reloading)**:React Native支持热重载功能,能在代码修改后快速更新应用状态,显著提高了开发效率。 5. **原生模块**:React Native允许开发者使用Java、Swift或Objective-C编写部分原生代码,...
By leveraging tools like Dreamweaver and Photoshop and utilizing advanced web technologies, a visually appealing and functional platform can be created to cater to the diverse needs of plush doll ...