Example usage for org.springframework.web.context.support AnnotationConfigWebApplicationContext setConfigLocations

List of usage examples for org.springframework.web.context.support AnnotationConfigWebApplicationContext setConfigLocations

Introduction

In this page you can find the example usage for org.springframework.web.context.support AnnotationConfigWebApplicationContext setConfigLocations.

Prototype

public void setConfigLocations(@Nullable String... locations) 

Source Link

Document

Set the config locations for this application context.

Usage

From source file:org.cloudfoundry.identity.uaa.test.IntegrationTestContextLoader.java

@Override
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
    if (!(mergedConfig instanceof WebMergedContextConfiguration)) {
        throw new IllegalArgumentException(
                String.format(/*from   w w  w.ja va2  s.  c  o m*/
                        "Cannot load WebApplicationContext from non-web merged context configuration %s. "
                                + "Consider annotating your test class with @WebAppConfiguration.",
                        mergedConfig));
    }

    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

    ApplicationContext parent = mergedConfig.getParentApplicationContext();
    if (parent != null) {
        context.setParent(parent);
    }
    context.setServletContext(new MockServletContext());
    context.setConfigLocations(mergedConfig.getLocations());
    context.register(mergedConfig.getClasses());
    new YamlServletProfileInitializer().initialize(context);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

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//w ww .  j  a  v a  2  s.c  o  m
        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;
}