Example usage for org.springframework.context.event ContextRefreshedEvent getApplicationContext

List of usage examples for org.springframework.context.event ContextRefreshedEvent getApplicationContext

Introduction

In this page you can find the example usage for org.springframework.context.event ContextRefreshedEvent getApplicationContext.

Prototype

public final ApplicationContext getApplicationContext() 

Source Link

Document

Get the ApplicationContext that the event was raised for.

Usage

From source file:ca.unx.template.Main.java

public static void main(String[] args) throws Exception {

    final Logger logger = LoggerFactory.getLogger("main");

    try {//from   w  w w  .  java 2 s.  co m
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        /*
         * One problem with SpringMVC is it creates its own application
         * context, and so it can end up failing but our application will
         * keep running.
         * 
         * To detect the case where the SpringMVC's web application context
         * fails we'll listen for ContextRefreshEvents and set a flag when
         * we see the web application context refresh.
         */
        applicationContext.addApplicationListener(new ApplicationListener<ContextRefreshedEvent>() {
            @Override
            public void onApplicationEvent(ContextRefreshedEvent event) {
                ApplicationContext ctx = event.getApplicationContext();
                if (ctx instanceof GenericWebApplicationContext) {
                    webApplicationContextInitialized = true;
                }
            }
        });

        applicationContext.registerShutdownHook();
        applicationContext.register(RootConfiguration.class);
        applicationContext.refresh();

        if (!webApplicationContextInitialized) {
            logger.error("Failed to initialize web application.  Exiting.");
            System.exit(1);
        }

        logger.info("Running.");
    } catch (Exception e) {
        logger.error("Error starting application", e);
        System.exit(1);
    }
}

From source file:com.tcloud.bee.key.server.jetty.SpringJettyServer.java

/**
 * @param args//ww w  .  ja v  a 2s.  com
 */
public static void main(String[] args) {

    logger.info("Start Spring Jetty Server.....");

    try {

        @SuppressWarnings("resource")
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        /*
         * One problem with SpringMVC is it creates its own application
         * context, and so it can end up failing but our application will
         * keep running.
         * 
         * To detect the case where the SpringMVC's web application context
         * fails we'll listen for ContextRefreshEvents and set a flag when
         * we see one.
         */
        applicationContext.addApplicationListener(new ApplicationListener<ContextRefreshedEvent>() {
            @Override
            public void onApplicationEvent(ContextRefreshedEvent event) {
                ApplicationContext ctx = event.getApplicationContext();
                if (ctx instanceof AnnotationConfigWebApplicationContext) {
                    webApplicationContextInitialized = true;
                }
            }
        });

        logger.info("Start register JettyConfiguration.....");
        applicationContext.registerShutdownHook();
        applicationContext.register(RootConfiguration.class);
        applicationContext.refresh();

        if (!webApplicationContextInitialized) {
            logger.error("Web application context not initialized. Exiting.");
            System.exit(1);
        }

        logger.info("Running.");
    } catch (Exception e) {
        logger.error("Error starting application", e);
        System.exit(1);
    }
}

From source file:org.web4thejob.context.ORMLauncher.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    if (event.getApplicationContext().getParent() == null) {
        if (ContextUtil.getSystemJoblet().isInstalled()) {
            if (!CollectionUtils.containsInstance(ContextUtil.getActiveProfiles(), "installed")) {
                ContextUtil.addActiveProfile("installed");
                ContextUtil.refresh();//from  w  w w  .  j av  a  2s  .c  om
            }
        }
    }
}

From source file:com.si.xe.trader.webui.init.RunDBInitializerWhenNeeded.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    DBInit init = event.getApplicationContext().getBean(DBInit.class);
    //init.createItemsIfNoUsersExist();
}

From source file:com.ai.bss.webui.datainit.RunDBInitializerWhenNeeded.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    DBInit init = event.getApplicationContext().getBean(DBInit.class);

    if ("Root WebApplicationContext".equals(event.getApplicationContext().getDisplayName())) {
        init.createItemsIfNoUsersExist();
    }//from   w w  w.  j  a va  2s.  c o m
}

From source file:se.softhouse.garden.orchid.spring.SpringBeanConfigurer.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    context = event.getApplicationContext();
    beanConfigurer = (BeanConfigurerSupport) event.getApplicationContext().getBean("beanConfigurer");
}

From source file:com.brienwheeler.apps.main.ContextClosingBean.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ((AbstractApplicationContext) event.getApplicationContext()).close();
}

From source file:nl.enovation.addressbook.cqrs.webui.init.RunDBInitializerWhenNeeded.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    DBInit init = event.getApplicationContext().getBean(DBInit.class);

    if ("Root WebApplicationContext".equals(event.getApplicationContext().getDisplayName())) {
        init.createItems();//from  w ww  . ja v a  2s.  c  om
        LOGGER.info("The database has been created and refreshed with some data.");
    }
}

From source file:com.pavikumbhar.javaheart.springconfiguration.ContextRefreshedEventHandler.java

@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

    String applicationName = contextRefreshedEvent.getApplicationContext().getApplicationName();
    String version = environment.getProperty("application.version");
    System.out.println(//from   ww w. java 2 s.  c om
            "[################ [Started-" + applicationName + " " + version + " version] ###########]");

}

From source file:kzht.gm.springframework.extension.context.PrototypeBeanInitializeListener.java

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    initializeAspectJWorld(event.getApplicationContext());
}