Example usage for org.springframework.web.servlet DispatcherServlet init

List of usage examples for org.springframework.web.servlet DispatcherServlet init

Introduction

In this page you can find the example usage for org.springframework.web.servlet DispatcherServlet init.

Prototype

public void init(ServletConfig config) throws ServletException 

Source Link

Document

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

Usage

From source file:com.github.carlomicieli.nerdmovies.MockWebApplicationContextLoader.java

@Override
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
    final MockServletContext servletContext = new MockServletContext(configuration.webapp(),
            new FileSystemResourceLoader());
    final MockServletConfig servletConfig = new MockServletConfig(servletContext, configuration.name());

    final AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext);
    webContext.getEnvironment().setActiveProfiles(mergedConfig.getActiveProfiles());
    webContext.setServletConfig(servletConfig);
    webContext.setConfigLocations(mergedConfig.getLocations());
    webContext.register(mergedConfig.getClasses());

    // Create a DispatcherServlet that uses the previously established
    // WebApplicationContext.
    @SuppressWarnings("serial")
    final DispatcherServlet dispatcherServlet = new DispatcherServlet() {
        @Override/*  ww w.j a v a 2  s.c  o  m*/
        protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
            return webContext;
        }
    };

    // Add the DispatcherServlet (and anything else you want) to the
    // context.
    // Note: this doesn't happen until refresh is called below.
    webContext.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
            beanFactory.registerResolvableDependency(DispatcherServlet.class, dispatcherServlet);
            // Register any other beans here, including a ViewResolver if
            // you are using JSPs.
        }
    });

    // Have the context notify the servlet every time it is refreshed.
    webContext.addApplicationListener(
            new SourceFilteringListener(webContext, new ApplicationListener<ContextRefreshedEvent>() {
                @Override
                public void onApplicationEvent(ContextRefreshedEvent event) {
                    dispatcherServlet.onApplicationEvent(event);
                }
            }));

    // Prepare the context.
    webContext.refresh();
    webContext.registerShutdownHook();

    // Initialize the servlet.
    dispatcherServlet.setContextConfigLocation("");
    dispatcherServlet.init(servletConfig);

    return webContext;
}

From source file:org.geoserver.test.GeoServerAbstractTestSupport.java

protected DispatcherServlet getDispatcher() throws Exception {
    // create an instance of the spring dispatcher
    ServletContext context = applicationContext.getServletContext();

    MockServletConfig config = new MockServletConfig();
    config.setServletContext(context);/*from ww w  . j a  v a 2s . com*/
    config.setServletName("dispatcher");

    DispatcherServlet dispatcher = new DispatcherServlet();

    dispatcher.setContextConfigLocation(
            GeoServerAbstractTestSupport.class.getResource("dispatcher-servlet.xml").toString());
    dispatcher.init(config);

    return dispatcher;
}