下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#stop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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
}
@Test
void shouldRetrieveNonLeasedSecret() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
VaultIntegrationTestConfiguration.class, NonRotatingSecret.class);
context.registerShutdownHook();
assertThat(context.getEnvironment().getProperty("my-key")).isEqualTo("my-value");
context.stop();
}
@Test
void shouldRetrieveRotatingSecret() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
VaultIntegrationTestConfiguration.class, RotatingSecret.class);
context.registerShutdownHook();
assertThat(context.getEnvironment().getProperty("my-key")).isEqualTo("my-value");
context.stop();
}
@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();
}
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();
}