3

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.
Community
  • 1
  • 1
JJ Zabkar
  • 3,792
  • 7
  • 45
  • 65

4 Answers4

2

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;
}
JJ Zabkar
  • 3,792
  • 7
  • 45
  • 65
  • Don't do it like that, as that is a really bad way. As mentioned in other posts the ideal place for this is an `ApplicationListener` which gets fired as soon as the context has finished loading. So instead of doing in that class and create the custom dispatcher servlet, just add a listener. – M. Deinum Feb 05 '16 at 12:01
1

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
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
0

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.

BeyelerStudios
  • 4,243
  • 19
  • 38
Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • I registered my `StartupListener` in my web.xml, but I get a RTE "Caused by: java.lang.ClassCircularityError: com/sun/beans/WeakCache". – JJ Zabkar Jun 19 '13 at 16:44
  • I also couldn't find a way to declare the listener in my `` declaration--a `` can only be declared under ``, right? How do I make it specific to the servlet context? – JJ Zabkar Jun 19 '13 at 17:04
  • You need to declare that in Spring servlet context... if you are using XML config, then put it there as a `` declaration. This has nothing to do with the standard based JEE Servlet listeners. – Pavel Horal Jun 19 '13 at 17:23
0

try this, change your port number. In my case i changed from server.port=8001 to server.port=8002