Java Code Examples for org.springframework.context.support.GenericApplicationContext#registerShutdownHook()

The following examples show how to use org.springframework.context.support.GenericApplicationContext#registerShutdownHook() . 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: SpringShutdownHookThreadDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    GenericApplicationContext context = new GenericApplicationContext();

    context.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
        @Override
        public void onApplicationEvent(ContextClosedEvent event) {
            System.out.printf("[线程 %s] ContextClosedEvent 处理\n", Thread.currentThread().getName());
        }
    });

    // 刷新 Spring 应用上下文
    context.refresh();

    // 注册 Shutdown Hook
    context.registerShutdownHook();

    System.out.println("按任意键继续并且关闭 Spring 应用上下文");
    System.in.read();

    // 关闭 Spring 应用(同步)
    context.close();
}
 
Example 2
Source File: SpringInitializer.java    From Mycat-openEP with Apache License 2.0 5 votes vote down vote up
public static GenericApplicationContext create(){
	GenericApplicationContext context=new GenericApplicationContext();
	initAnnotatedConf(context);
	initXmlConf(context);
	initGroovyConf(context);
	initPropertiesConf(context);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 3
Source File: AbstractGenericContextLoader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@code locations}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #createBeanDefinitionReader(GenericApplicationContext)} to create a
 * {@link BeanDefinitionReader} which is then used to populate the context
 * from the specified locations.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * <p><b>Note</b>: this method does not provide a means to set active bean definition
 * profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)}
 * and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * for an alternative.
 *
 * @return a new application context
 * @see org.springframework.test.context.ContextLoader#loadContext
 * @see GenericApplicationContext
 * @see #loadContext(MergedContextConfiguration)
 * @since 2.5
 */
@Override
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for locations [%s].",
			StringUtils.arrayToCommaDelimitedString(locations)));
	}
	GenericApplicationContext context = new GenericApplicationContext();
	prepareContext(context);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	createBeanDefinitionReader(context).loadBeanDefinitions(locations);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 4
Source File: AbstractGenericContextLoader.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@code locations}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #createBeanDefinitionReader(GenericApplicationContext)} to create a
 * {@link BeanDefinitionReader} which is then used to populate the context
 * from the specified locations.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * <p><b>Note</b>: this method does not provide a means to set active bean definition
 * profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)}
 * and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * for an alternative.
 *
 * @return a new application context
 * @see org.springframework.test.context.ContextLoader#loadContext
 * @see GenericApplicationContext
 * @see #loadContext(MergedContextConfiguration)
 * @since 2.5
 */
@Override
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for locations [%s].",
			StringUtils.arrayToCommaDelimitedString(locations)));
	}
	GenericApplicationContext context = new GenericApplicationContext();
	prepareContext(context);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	createBeanDefinitionReader(context).loadBeanDefinitions(locations);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 5
Source File: AbstractGenericContextLoader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} for backwards
 * compatibility with the {@link org.springframework.test.context.ContextLoader
 * ContextLoader} SPI.</li>
 * <li>Calls {@link #prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * to allow for customizing the context before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)}
 * to populate the context from the locations or classes in the supplied
 * {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * @return a new application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericApplicationContext
 * @since 3.1
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
			mergedConfig));
	}

	validateMergedContextConfiguration(mergedConfig);

	GenericApplicationContext context = new GenericApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	prepareContext(context);
	prepareContext(context, mergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	loadBeanDefinitions(context, mergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 6
Source File: AbstractGenericContextLoader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@code locations}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #createBeanDefinitionReader(GenericApplicationContext)} to create a
 * {@link BeanDefinitionReader} which is then used to populate the context
 * from the specified locations.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * <p><b>Note</b>: this method does not provide a means to set active bean definition
 * profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)}
 * and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * for an alternative.
 *
 * @return a new application context
 * @see org.springframework.test.context.ContextLoader#loadContext
 * @see GenericApplicationContext
 * @see #loadContext(MergedContextConfiguration)
 * @since 2.5
 */
@Override
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for locations [%s].",
			StringUtils.arrayToCommaDelimitedString(locations)));
	}
	GenericApplicationContext context = new GenericApplicationContext();
	prepareContext(context);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	createBeanDefinitionReader(context).loadBeanDefinitions(locations);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 7
Source File: AbstractGenericContextLoader.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} for backwards
 * compatibility with the {@link org.springframework.test.context.ContextLoader
 * ContextLoader} SPI.</li>
 * <li>Calls {@link #prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * to allow for customizing the context before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)}
 * to populate the context from the locations or classes in the supplied
 * {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>Calls {@link #customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)} to
 * allow for customizing the context before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * @return a new application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericApplicationContext
 * @since 3.1
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
			mergedConfig));
	}

	validateMergedContextConfiguration(mergedConfig);

	GenericApplicationContext context = new GenericApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	prepareContext(context);
	prepareContext(context, mergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	loadBeanDefinitions(context, mergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	customizeContext(context, mergedConfig);
	context.refresh();
	context.registerShutdownHook();
	return context;
}
 
Example 8
Source File: AbstractGenericContextLoader.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}.
 *
 * <p>Implementation details:
 *
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} for backwards
 * compatibility with the {@link org.springframework.test.context.ContextLoader
 * ContextLoader} SPI.</li>
 * <li>Calls {@link #prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * to allow for customizing the context before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)}
 * to populate the context from the locations or classes in the supplied
 * {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>Calls {@link #customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)} to
 * allow for customizing the context before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * @return a new application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericApplicationContext
 * @since 3.1
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
			mergedConfig));
	}

	validateMergedContextConfiguration(mergedConfig);

	GenericApplicationContext context = new GenericApplicationContext();

	ApplicationContext parent = mergedConfig.getParentApplicationContext();
	if (parent != null) {
		context.setParent(parent);
	}
	prepareContext(context);
	prepareContext(context, mergedConfig);
	customizeBeanFactory(context.getDefaultListableBeanFactory());
	loadBeanDefinitions(context, mergedConfig);
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	customizeContext(context);
	customizeContext(context, mergedConfig);
	context.refresh();
	context.registerShutdownHook();
	return context;
}