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

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

Introduction

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

Prototype

public void setContextConfigLocation(@Nullable String contextConfigLocation) 

Source Link

Document

Set the context config location explicitly, instead of relying on the default location built from the namespace.

Usage

From source file:com.techtrip.dynbl.context.config.WebAppinitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    // Setup Context to Accept Annotated Classes on Input (including plain Spring {@code @Component}
    // Stereotypes in addition to JSR-330 Compliant Classes using {@code javax.inject}
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

    //context.setConfigLocation(APP_CONFIG_LOCATION);
    context.setConfigLocation(APP_CONFIG_LOCATION);

    /* // ww  w  .  j  a  v a 2s  .co  m
    * Add a Spring Security Filter using the JEE6 Filter Registration Filter Method from {@code FilterRegistration) that allows filters to be registered
    *  and configured with the specified context
    */
    /*              
    FilterRegistration.Dynamic securityFilter = servletContext.addFilter(ProjectKeyValConsts.SECURITY_FILTER.getKey(), new DelegatingFilterProxy(ProjectKeyValConsts.SECURITY_FILTER.getValue()));
    securityFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, ProjectConsts.BASE_URL_MAPPING_PATTERN.getValue()); // where the filter will be applied
    */
    // Add a Character Encoding Filter that specifies an encoding for mapped requests
    FilterRegistration.Dynamic characterEncodingFilter = servletContext.addFilter("characterEncodingFilter",
            new CharacterEncodingFilter());
    characterEncodingFilter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, ROOT_CONTEXT); // where the filter will be applied
    characterEncodingFilter.setInitParameter("encoding", "UTF-8");
    characterEncodingFilter.setInitParameter("forceEncoding", Boolean.TRUE.toString());
    characterEncodingFilter.setAsyncSupported(true);

    servletContext.addListener(new ContextLoaderListener(context));
    servletContext.setInitParameter("defaultHtmlEscape", Boolean.TRUE.toString());

    DispatcherServlet servlet = new DispatcherServlet();

    // no explicit configuration reference here: everything is configured in the root container for simplicity
    servlet.setContextConfigLocation("");

    /* TMT From JEE 6 API Docs:
    * Registers the given servlet instance with this ServletContext under the given servletName.
    * The registered servlet may be further configured via the returned ServletRegistration object. 
    */

    ServletRegistration.Dynamic appServlet = servletContext.addServlet(APP_SERVLET, servlet);
    appServlet.setLoadOnStartup(1);
    appServlet.setAsyncSupported(true);

    Set<String> mappingConflicts = appServlet.addMapping("/");

    if (!mappingConflicts.isEmpty()) {
        throw new IllegalStateException(String.format(
                "The servlet named '%s' cannot be mapped to '/' under Tomcat versions <= 7.0.14", APP_SERVLET));
    }

    // TMT
    servletContext.addListener(new Log4jConfigListener());

    System.out.println("Application inplemented on Spring Version: " + SpringVersion.getVersion());

}

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//from   ww w  .  j  a va 2s  .  c  om
        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   w w w  . jav  a2  s.  c  o  m*/
    config.setServletName("dispatcher");

    DispatcherServlet dispatcher = new DispatcherServlet();

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

    return dispatcher;
}