Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext addApplicationListener

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext addApplicationListener

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext addApplicationListener.

Prototype

@Override
    public void addApplicationListener(ApplicationListener<?> listener) 

Source Link

Usage

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

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

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

    try {/*ww w .j av a2 s . c o 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//  w  ww . j av  a2  s. c om
 */
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);
    }
}