Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Web Application: Post-DispatcherServlet initialization

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.
like image 227
JJ Zabkar Avatar asked Jul 05 '26 15:07

JJ Zabkar


2 Answers

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;
}
like image 153
JJ Zabkar Avatar answered Jul 07 '26 06:07

JJ Zabkar


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
like image 24
acdcjunior Avatar answered Jul 07 '26 07:07

acdcjunior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!