Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#stop()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#stop() . These examples are extracted from open source projects. 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
public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    // 将引导类 ApplicationListenerDemo 作为 Configuration Class
    context.register(ApplicationListenerDemo.class);

    // 方法一:基于 Spring 接口:向 Spring 应用上下文注册事件
    // a 方法:基于 ConfigurableApplicationContext API 实现
    context.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            println("ApplicationListener - 接收到 Spring 事件:" + event);
        }
    });

    // b 方法:基于 ApplicationListener 注册为 Spring Bean
    // 通过 Configuration Class 来注册
    context.register(MyApplicationListener.class);

    // 方法二:基于 Spring 注解:@org.springframework.context.event.EventListener

    // ApplicationEventMulticaster
    // 启动 Spring 应用上下文
    context.refresh(); // ContextRefreshedEvent
    // 启动 Spring 上下文
    context.start();  // ContextStartedEvent
    // 停止 Spring 上下文
    context.stop();  // ContextStoppedEvent
    // 关闭 Spring 应用上下文
    context.close(); // ContextClosedEvent
}
 
Example 2
@Test
void shouldRetrieveNonLeasedSecret() {

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			VaultIntegrationTestConfiguration.class, NonRotatingSecret.class);
	context.registerShutdownHook();

	assertThat(context.getEnvironment().getProperty("my-key")).isEqualTo("my-value");

	context.stop();
}
 
Example 3
@Test
void shouldRetrieveRotatingSecret() {

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			VaultIntegrationTestConfiguration.class, RotatingSecret.class);
	context.registerShutdownHook();

	assertThat(context.getEnvironment().getProperty("my-key")).isEqualTo("my-value");

	context.stop();
}
 
Example 4
@Test
void namespacesSupportedThroughConfiguration() {

	namespaceSecretsAreIsolated();

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			NamespaceConfiguration.class);
	VaultOperations operations = context.getBean(VaultOperations.class);
	assertThat(operations.read("marketing-secrets/my-secret")).isNotNull();

	context.stop();
}
 
Example 5
public static void main(String[] args) {

		VaultInitializer initializer = new VaultInitializer();
		initializer.initialize();

		PrepareVault prepareVault = initializer.prepare();
		VaultOperations vaultOperations = prepareVault.getVaultOperations();

		Map<String, String> data = new HashMap<String, String>();
		data.put("encrypted", "Much secret. Very confidential. Wow.");

		vaultOperations.write("secret/secure-introduction", data);

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

		System.out.println(context.getEnvironment().getProperty("my-property-that-references-vault"));
		System.out.println(context.getEnvironment().getProperty("encrypted"));

		System.out.println(context.getBean(Client.class).myValue);

		context.stop();
	}