com.kajj.tools.logviewer.config.LogViewerWebApplicationInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.kajj.tools.logviewer.config.LogViewerWebApplicationInitializer.java

Source

/*
 * Copyright 2014 Andrew Tytula.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kajj.tools.logviewer.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * Replacement for web.xml
 *
 * @author Andrew Tytula
 */
public class LogViewerWebApplicationInitializer implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "mvc-dispatcher";
    private static final String MAPPING_ANY = "/";

    /**
     * Initialization method for the ServletContext. Creates the Spring context and
     * DispatcherServlet.
     *
     * @param servletContext The ServletContext to initialize.
     * @throws ServletException If the ServletContext fails to initialize.
     */
    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        final AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
        springContext.setConfigLocation("com.kajj.tools.logviewer.config");
        servletContext.addListener(new ContextLoaderListener(springContext));

        final DispatcherServlet servlet = new DispatcherServlet(springContext);
        final ServletRegistration.Dynamic dispatcher = servletContext.addServlet(DISPATCHER_SERVLET_NAME, servlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(MAPPING_ANY);
    }
}