com.azirar.requester.ws.config.WebAppInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.azirar.requester.ws.config.WebAppInitializer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azirar.requester.ws.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.stereotype.Component;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.azirar.requester.ws.CorsFilter;

@Component
public class WebAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();

        dispatcherServlet.register(AppConf.class);

        dispatcherServlet.setServletContext(servletContext);

        //        //Added filter dynamically
        javax.servlet.FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter",
                CorsFilter.class);
        corsFilter.addMappingForUrlPatterns(null, true, "/*");

        Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);
    }

}