Spring Web Application: Post-DispatcherServlet initialization

隐身守侯 提交于 2019-12-23 16:53:43

问题


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?

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:

  1. Extending ContextLoaderListener/implementing ServletContextListener per this answer.
  2. Implementing WebApplicationInitializer per the javaoc.
  3. 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.

回答1:


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;
}



回答2:


From Spring's Standard and Custom Events.

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextListener implements
                                     ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("ApplicationContext was initialized or refreshed: "
                               + event.getApplicationContext().getDisplayName());
    }

}

The event above will be fired when the DispatcherServlet is initialized, such as when it prints:

INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'ServletName': initialization completed in 1234 ms



回答3:


You can implement ApplicationListener<ContextStartedEvent> within your application context. This event listener will then be called once for your root context and once for each servlet context.

public class StartupListener implements ApplicationListener<ContextStartedEvent> {

    public void onApplicationEvent(ContextStartedEvent event) {
        ApplicationContext context = (ApplicationContext) event.getSource();
        System.out.println("Context '" + context.getDisplayName() + "' started.");
    }

}

If you define this listener within your servlet context, it should be called just once for the servlet context intself.



来源:https://stackoverflow.com/questions/17195258/spring-web-application-post-dispatcherservlet-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!