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

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

Introduction

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

Prototype

@Override
public void setApplicationContext(ApplicationContext applicationContext) 

Source Link

Document

Called by Spring via ApplicationContextAware to inject the current application context.

Usage

From source file:com.cfitzarl.cfjwed.Main.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Add spring security filter chain
    servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class)
            .addMappingForUrlPatterns(null, false, "/*");

    // Add Hibernate session binder
    servletContext.addFilter("jpaTransactionFilter", OpenEntityManagerInViewFilter.class)
            .addMappingForUrlPatterns(null, false, "/*");

    // Define application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(WebApplicationMvcConfigurer.class);
    rootContext.scan("com.cfitzarl.cfjwed");

    // Create a dispatcher servlet that loads the application context
    DispatcherServlet dispatcherServlet = new DispatcherServlet();
    dispatcherServlet.setApplicationContext(rootContext);

    // Add the dispatcher servlet to the servlet context and map it to /
    ServletRegistration.Dynamic servletRegister = servletContext.addServlet("dispatcher", dispatcherServlet);
    servletRegister.setLoadOnStartup(1);
    servletRegister.addMapping("/");

    servletContext.addListener(new ContextLoaderListener(rootContext));
}