org.springframework.context.event.ContextStartedEvent Java Examples

The following examples show how to use org.springframework.context.event.ContextStartedEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApplicationEventListener.java    From SpringBoot-Base-System with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 应用程序启动过程监听
 * 
 * @time 2018年4月10日 下午5:05:33
 * @version V1.0
 * @param event
 */
@Override
public void onApplicationEvent(ApplicationEvent event) {
	// 在这里可以监听到Spring Boot的生命周期
	if (event instanceof ApplicationEnvironmentPreparedEvent) { // 初始化环境变量
		log.debug("初始化环境变量");
	} else if (event instanceof ApplicationPreparedEvent) { // 初始化完成
		log.debug("初始化环境变量完成");
	} else if (event instanceof ContextRefreshedEvent) { // 应用刷新,当ApplicationContext初始化或者刷新时触发该事件。
		log.debug("应用刷新");
	} else if (event instanceof ApplicationReadyEvent) {// 应用已启动完成
		log.debug("应用已启动完成");
	} else if (event instanceof ContextStartedEvent) { // 应用启动,Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的
														// Start()方法开始/重新开始容器时触发该事件。
		log.debug("应用启动");
	} else if (event instanceof ContextStoppedEvent) { // 应用停止,Spring2.5新增的事件,当容器调用ConfigurableApplicationContext
														// 的Stop()方法停止容器时触发该事件。
		log.debug("应用停止");
	} else if (event instanceof ContextClosedEvent) { // 应用关闭,当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有
														// 单例Bean都被销毁。
		log.debug("应用关闭");
	} else {
	}
}
 
Example #2
Source File: CommonAnnotationBeanPostProcessor.java    From brpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * Do annotation on class type resolve action after spring container started.
 * 
 * @param event spring application event
 */
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextStartedEvent || event instanceof ContextRefreshedEvent) {

        // only execute this method once. bug fix for ContextRefreshedEvent will invoke twice on spring MVC servlet
        if (started.compareAndSet(false, true)) {
            for (BeanInfo bean : typeAnnotationedBeans) {
                if (getCallback() != null) {
                    Object targetBean = beanFactory.getBean(bean.name);
                    getCallback().annotationAtTypeAfterStarted(bean.annotation, targetBean, bean.name, beanFactory);
                }
            }
        } else {
            LOGGER.warn("onApplicationEvent of application event [" + event
                    + "] ignored due to processor already started.");
        }

    }

}
 
Example #3
Source File: ApplicationEventListener.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        LOG.debug("初始化环境变量");
    } else if (event instanceof ApplicationPreparedEvent) {
        LOG.debug("初始化完成");
        LOG.debug("初始GameData策划数据");
        String path = ApplicationEventListener.class.getResource("/gamedata").getFile();
        ConfigManager.loadGameData(path);
    } else if (event instanceof ContextRefreshedEvent) {
        LOG.debug("应用刷新");
    } else if (event instanceof ApplicationReadyEvent) {
        LOG.debug("应用已启动完成");
    } else if (event instanceof ContextStartedEvent) {
        LOG.debug("应用启动,需要在代码动态添加监听器才可捕获");
    } else if (event instanceof ContextStoppedEvent) {
        LOG.debug("应用停止");
    } else if (event instanceof ContextClosedEvent) {
        ApplicationContext applicationContext = ((ContextClosedEvent) event).getApplicationContext();
        grpcClient = applicationContext.getBean(GrpcClient.class);
        grpcClient.close();
        LOG.debug("应用关闭");
    }
}
 
Example #4
Source File: ApplicationEventListener.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        LOG.debug("初始化环境变量");
    } else if (event instanceof ApplicationPreparedEvent) {
        LOG.debug("初始化完成");
        LOG.debug("初始GameData策划数据");
        ConfigManager.loadGameData(this.configPath);
    } else if (event instanceof ContextRefreshedEvent) {
        LOG.debug("应用刷新");
    } else if (event instanceof ApplicationReadyEvent) {
        LOG.debug("应用已启动完成");
    } else if (event instanceof ContextStartedEvent) {
        LOG.debug("应用启动,需要在代码动态添加监听器才可捕获");
    } else if (event instanceof ContextStoppedEvent) {
        LOG.debug("应用停止");
    } else if (event instanceof ContextClosedEvent) {
        ApplicationContext context = ((ContextClosedEvent) event).getApplicationContext();
        rpcClient = context.getBean(RpcClient.class);
        rpcClient.close();
        LOG.error("应用关闭");
    } else {

    }
}
 
Example #5
Source File: ApplicationEventListener.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ApplicationEnvironmentPreparedEvent) {
        LOGGER.debug("初始化环境变量");
    } else if (event instanceof ApplicationPreparedEvent) {
        LOGGER.debug("初始化完成");
    } else if (event instanceof ContextRefreshedEvent) {
        LOGGER.debug("应用刷新");
    } else if (event instanceof ApplicationReadyEvent) {
        LOGGER.debug("应用已启动完成");
    } else if (event instanceof ContextStartedEvent) {
        LOGGER.debug("应用启动,需要在代码动态添加监听器才可捕获");
    } else if (event instanceof ContextStoppedEvent) {
        LOGGER.debug("应用停止");
    } else if (event instanceof ContextClosedEvent) {
        ApplicationContext applicationContext = ((ContextClosedEvent) event).getApplicationContext();
        grpcClient = applicationContext.getBean(GrpcClient.class);
        grpcClient.close();
        LOGGER.debug("应用关闭");
    }

}
 
Example #6
Source File: ContextStartedEventListener.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ContextStartedEvent event)
{
    ApplicationContext applicationContext = event.getApplicationContext();

    AllureLogAppender allureLogAppender = AllureLogAppender.getInstance();
    allureLogAppender.setAllureStoryReporter(applicationContext.getBean(AllureStoryReporter.class));
}
 
Example #7
Source File: MetricServer.java    From realtime-analytics with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {

    if (event instanceof ContextStartedEvent) {
        this.startStandAlone();
    } else if (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent) {
        this.stopStandAlone();
    }
}
 
Example #8
Source File: ContextEventAdapter.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(Object event) {
    if (event instanceof StartupEvent) {
        eventPublisher.publishEvent(new ContextStartedEvent(
            applicationContext
        ));
    } else if (event instanceof ShutdownEvent) {
        eventPublisher.publishEvent(new ContextClosedEvent(
                applicationContext
        ));
    }
}
 
Example #9
Source File: Context.java    From document-management-software with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Processes a newly incoming event on appropriated events that registered
 * itself on it
 * 
 * @param event the event to process
 */
@Override
public synchronized void onApplicationEvent(ApplicationEvent event) {
	if ((event instanceof ContextStartedEvent) || (event instanceof ContextRefreshedEvent)) {
		processEvents(SystemEventStatus.BEANS_AVAILABLE);
	} else if (event instanceof ContextClosedEvent) {
		processEvents(SystemEventStatus.SYSTEM_DESTORY);
	}

}
 
Example #10
Source File: StartContextListenerTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldIgnoreNonExistentCleanableDirectories(@TempDir Path tempDir)
{
    StartContextListener startContextListener = new StartContextListener();
    startContextListener.setCleanableDirectories(Optional.of(List.of(tempDir.resolve("non-existent").toFile())));
    startContextListener.onApplicationEvent(mock(ContextStartedEvent.class));
    assertTrue(Files.exists(tempDir));
}
 
Example #11
Source File: StartContextListenerTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void shouldDeleteCleanableDirectories(@TempDir Path tempDir) throws IOException
{
    Path dir = tempDir.resolve("to-be-cleaned-up");
    Files.createDirectories(dir);
    StartContextListener startContextListener = new StartContextListener();
    startContextListener.setCleanableDirectories(Optional.of(List.of(dir.toFile())));
    startContextListener.onApplicationEvent(mock(ContextStartedEvent.class));
    assertTrue(Files.exists(tempDir));
    assertFalse(Files.exists(dir));
}
 
Example #12
Source File: AbstractApplicationContext.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void start() {
	getLifecycleProcessor().start();
	publishEvent(new ContextStartedEvent(this));
}
 
Example #13
Source File: ApplicationListenerDemo.java    From geekbang-lessons with Apache License 2.0 4 votes vote down vote up
@EventListener
public void onApplicationEvent(ContextStartedEvent event) {
    println("@EventListener - 接收到 Spring ContextStartedEvent");
}
 
Example #14
Source File: ContextStartedListener.java    From ext-opensource-netty with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ContextStartedEvent event) {
	System.err.println("============Start 执行=========== ");
}
 
Example #15
Source File: AbstractApplicationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void start() {
	getLifecycleProcessor().start();
	publishEvent(new ContextStartedEvent(this));
}
 
Example #16
Source File: MyNamedService.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
@EventListener
public void onStartup(ContextStartedEvent startedEvent) {
    this.lastEvent = startedEvent;
}
 
Example #17
Source File: MyNamedService.java    From micronaut-spring with Apache License 2.0 4 votes vote down vote up
public ContextStartedEvent getLastEvent() {
    return lastEvent;
}
 
Example #18
Source File: AbstractApplicationContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void start() {
	getLifecycleProcessor().start();
	publishEvent(new ContextStartedEvent(this));
}
 
Example #19
Source File: AbstractApplicationContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void start() {
	getLifecycleProcessor().start();
	publishEvent(new ContextStartedEvent(this));
}
 
Example #20
Source File: StartContextListener.java    From vividus with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ContextStartedEvent event)
{
    cleanableDirectories.stream().flatMap(List::stream).forEach(this::deleteDirectory);
}
 
Example #21
Source File: PipelineStartupListener.java    From aliada-tool with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final ContextStartedEvent event) {
	LOGGER.info(MessageCatalog._00022_PIPELINE_STARTING);
	
	LOGGER.info(MessageCatalog._00023_PIPELINE_STARTED);
}
 
Example #22
Source File: ContextEventListener.java    From tutorials with MIT License 4 votes vote down vote up
@Order(2)
@EventListener
public void handleContextRefreshEvent(ContextStartedEvent ctxStartEvt) {
    System.out.println("Context Start Event received.");
}
 
Example #23
Source File: ContextEventListener.java    From tutorials with MIT License 4 votes vote down vote up
@Order(1)
@EventListener(classes = { ContextStartedEvent.class, ContextStoppedEvent.class })
public void handleMultipleEvents() {
    System.out.println("Multi-event listener invoked");
}
 
Example #24
Source File: AnnotationDrivenEventListener.java    From tutorials with MIT License 4 votes vote down vote up
@EventListener
public void handleContextStart(final ContextStartedEvent cse) {
    System.out.println("Handling context started event.");
    hitContextStartedHandler = true;
}