com.miserablemind.butter.bootstrap.AppInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.miserablemind.butter.bootstrap.AppInitializer.java

Source

package com.miserablemind.butter.bootstrap;

import com.miserablemind.butter.apps.butterApp.ButterAppMVCContext;
import com.miserablemind.butter.security.WebSecurityContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.Log4jConfigListener;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

/*
 * Miserable Mind
 * http://www.butter.miserablemind.com
 * The MIT License (MIT)
 */

/**
 * Main system initializer that creates all the contexts and filter chains.
 * It replaces XML configuration files from older Spring versions.
 *
 * @author <a href="http://www.miserablemind.com" target="_blank">miserablemind</a>
 */
public class AppInitializer implements WebApplicationInitializer {

    /**
     * Implements {@link WebApplicationInitializer} onStartup to initialize the system.
     * It registers and configures {@link RootContext}, {@link WebSecurityContext}, and app specific contexts.
     * It also initializes the {@link Log4jConfigListener}.
     *
     * @param container ServletContext
     */
    @Override
    public void onStartup(ServletContext container) {

        //Make the logger read settings from main properties file
        container.setInitParameter("log4jConfigLocation", "classpath:system.properties");
        container.addListener(new Log4jConfigListener());

        //Root Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContext.class);
        container.addListener(new ContextLoaderListener(rootContext));

        //Spring Security context
        AnnotationConfigWebApplicationContext securityContext = new AnnotationConfigWebApplicationContext();
        securityContext.register(WebSecurityContext.class);
        DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("springSecurityFilterChain");
        container.addFilter("springSecurityFilterChain", filterProxy).addMappingForUrlPatterns(null, false, "*");

        //Dispatcher Servlet context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(ButterAppMVCContext.class);

        //Register and map the dispatcher servlet
        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
        ServletRegistration.Dynamic dispatcher = container.addServlet("mvc-dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

}