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

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

Introduction

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

Prototype

@Override
public void registerShutdownHook() 

Source Link

Document

Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Usage

From source file:org.jbr.taskmgr.config.WebInitializer.java

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

    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.setParent(containerContext);
    dispatcherContext.register(WebMvcConfig.class);
    dispatcherContext.registerShutdownHook();

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);/*from  w w w  .j a  v  a  2s . c o m*/
    dispatcher.addMapping("/");
}

From source file:sindica.to.dropwizard.spring.BaseSpringApplication.java

@Override
public final void run(T configuration, Environment environment) throws ClassNotFoundException {
    AnnotationConfigWebApplicationContext parent = new AnnotationConfigWebApplicationContext();
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

    parent.refresh();//  www  . ja  va 2  s .com
    parent.getBeanFactory().registerSingleton("configuration", configuration);
    parent.registerShutdownHook();
    parent.start();

    //the real main app context has a link to the parent context
    ctx.setParent(parent);

    onConfigureSpringContext(ctx);

    ctx.refresh();
    ctx.registerShutdownHook();
    ctx.start();

    //HealthChecks
    Map<String, HealthCheck> healthChecks = ctx.getBeansOfType(HealthCheck.class);
    for (Map.Entry<String, HealthCheck> entry : healthChecks.entrySet()) {
        LOG.info("Registering HealthCheck: ${entry.key}");
        environment.healthChecks().register(entry.getKey(), entry.getValue());
    }

    //resources
    Map<String, Object> resources = ctx.getBeansWithAnnotation(Path.class);
    for (Map.Entry<String, Object> entry : resources.entrySet()) {
        LOG.info("Registering Resource: ${entry.key}");
        environment.jersey().register(entry.getValue());
    }

    //tasks
    Map<String, Task> tasks = ctx.getBeansOfType(Task.class);
    for (Map.Entry<String, Task> entry : tasks.entrySet()) {
        LOG.info("Registering Task: ${entry.key}");
        environment.admin().addTask(entry.getValue());
    }

    //Managed
    Map<String, Managed> managers = ctx.getBeansOfType(Managed.class);
    for (Map.Entry<String, Managed> entry : managers.entrySet()) {
        LOG.info("Registering Task: ${entry.key}");
        environment.lifecycle().manage(entry.getValue());
    }

    //JAX-RS providers
    Map<String, Object> providers = ctx.getBeansWithAnnotation(Provider.class);
    for (Map.Entry<String, Object> entry : providers.entrySet()) {
        LOG.info("Registering Provider: ${entry.key}");
        //environment.jersey().addProvider(entry.getValue())
    }

    //last, but not least, let's link Spring to the embedded Jetty in Dropwizard
    EventListener servletListener = new SpringContextLoaderListener(ctx);
    environment.servlets().addServletListeners(servletListener);

    onRun(configuration, environment, ctx);
}

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(/* www.ja  v a2s  .  c om*/
                        "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/*ww w .  j av  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;
}