问题描述
我正在使用Spring 3.2 DispatcherServlet.我正在寻找在初始化完成后 发生的初始化钩子;标准的Spring解决方案或servlet解决方案.有什么建议吗?
I am using Spring 3.2 DispatcherServlet. I am looking for an initialization hook that takes place after the DispatcherServlet initialization completes; either a standard Spring solution or servlet solution. Any suggestions?
作为参考,遵循Servlet启动后的最终日志记录语句.我希望我的初始化方法在configured successfully 日志语句.
As a point of reference, the final logging statements after servlet startup follow. I want my initialization method to execute right after the configured successfully log statement.
DEBUG o.s.w.s.DispatcherServlet - Published WebApplicationContext of servlet 'mySpringDispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.mySpringDispatcherServlet] INFO o.s.w.s.DispatcherServlet - FrameworkServlet 'mySpringDispatcherServlet': initialization completed in 5000 ms DEBUG o.s.w.s.DispatcherServlet - Servlet 'mySpringDispatcherServlet' configured successfully
根据我的研究,到目前为止,以下内容尚未达到预期的效果:
From my research, so far the following have not had the desired effect:
- 根据此答案扩展ContextLoaderListener/实施ServletContextListener.
- 每个
rel ="nofollow noreferrer > javaoc . - 我的豆子成功使用@PostConstruct;我正在寻找一个Servlet或容器级别的钩子,该钩子将在容器初始化和对bean进行后处理之后基本上执行.
- Extending ContextLoaderListener/implementing ServletContextListener per this answer.
- Implementing WebApplicationInitializer per the javaoc.
- My beans use @PostConstruct successfully; I'm looking for a Servlet or container level hook that will be executed essentially after the container initializes and post-processes the beans.
推荐答案
根本问题是我无法覆盖final方法 HttpsServlet.init() .我在DispatcherServlet.initWebApplicationContext中找到了附近的@Override -able方法,可确保我的bean和上下文已完全初始化:
The root issue was that I couldn't override the final method HttpsServlet.init(). I found a nearby @Override-able method in DispatcherServlet.initWebApplicationContext that ensured my beans and context were fully initialized:
@Override protected WebApplicationContext initWebApplicationContext() { WebApplicationContext wac = super.initWebApplicationContext(); // do stuff with initialized Foo beans via: // wac.getBean(Foo.class); return result; }
这篇关于Spring Web应用程序:Post-DispatcherServlet初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!