Java 类org.springframework.beans.factory.support.DefaultSingletonBeanRegistry 实例源码

项目:spring-cloud-stream    文件:AbstractMessageChannelBinder.java   
private void destroyErrorInfrastructure(ProducerDestination destination) {
    String errorChannelName = errorsBaseName(destination);
    String errorBridgeHandlerName = getErrorBridgeName(destination);
    MessageHandler bridgeHandler = null;
    if (getApplicationContext().containsBean(errorBridgeHandlerName)) {
        bridgeHandler = getApplicationContext().getBean(errorBridgeHandlerName, MessageHandler.class);
    }
    if (getApplicationContext().containsBean(errorChannelName)) {
        SubscribableChannel channel = getApplicationContext().getBean(errorChannelName, SubscribableChannel.class);
        if (bridgeHandler != null) {
            channel.unsubscribe(bridgeHandler);
            ((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
                    .destroySingleton(errorBridgeHandlerName);
        }
        ((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
                .destroySingleton(errorChannelName);
    }
}
项目:class-guard    文件:SharedBeanRegistryTests.java   
@Test
public void testDisposableBean() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();

    DerivedTestBean tb = new DerivedTestBean();
    beanRegistry.registerSingleton("tb", tb);
    beanRegistry.registerDisposableBean("tb", tb);
    assertSame(tb, beanRegistry.getSingleton("tb"));

    assertSame(tb, beanRegistry.getSingleton("tb"));
    assertEquals(1, beanRegistry.getSingletonCount());
    assertEquals(1, beanRegistry.getSingletonNames().length);
    assertTrue(Arrays.asList(beanRegistry.getSingletonNames()).contains("tb"));
    assertFalse(tb.wasDestroyed());

    beanRegistry.destroySingletons();
    assertEquals(0, beanRegistry.getSingletonCount());
    assertEquals(0, beanRegistry.getSingletonNames().length);
    assertTrue(tb.wasDestroyed());
}
项目:spring-cloud-stream    文件:AbstractMessageChannelBinder.java   
private void destroyErrorInfrastructure(ConsumerDestination destination, String group, C properties) {
    try {
        String recoverer = getErrorRecovererName(destination, group, properties);
        if (getApplicationContext().containsBean(recoverer)) {
            ((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory()).destroySingleton(recoverer);
        }
        String errorChannelName = errorsBaseName(destination, group, properties);
        String errorMessageHandlerName = getErrorMessageHandlerName(destination, group, properties);
        String errorBridgeHandlerName = getErrorBridgeName(destination, group, properties);
        MessageHandler bridgeHandler = null;
        if (getApplicationContext().containsBean(errorBridgeHandlerName)) {
            bridgeHandler = getApplicationContext().getBean(errorBridgeHandlerName, MessageHandler.class);
        }
        MessageHandler handler = null;
        if (getApplicationContext().containsBean(errorMessageHandlerName)) {
            handler = getApplicationContext().getBean(errorMessageHandlerName, MessageHandler.class);
        }
        if (getApplicationContext().containsBean(errorChannelName)) {
            SubscribableChannel channel = getApplicationContext().getBean(errorChannelName, SubscribableChannel.class);
            if (bridgeHandler != null) {
                channel.unsubscribe(bridgeHandler);
                ((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
                        .destroySingleton(errorBridgeHandlerName);
            }
            if (handler != null) {
                channel.unsubscribe(handler);
                ((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
                        .destroySingleton(errorMessageHandlerName);
            }
            ((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
                    .destroySingleton(errorChannelName);
        }
    }
    catch (IllegalStateException e) {
        // context is shutting down.
    }
}
项目:class-guard    文件:SharedBeanRegistryTests.java   
@Test
public void testSingletons() {
    DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();

    TestBean tb = new TestBean();
    beanRegistry.registerSingleton("tb", tb);
    assertSame(tb, beanRegistry.getSingleton("tb"));

    TestBean tb2 = (TestBean) beanRegistry.getSingleton("tb2", new ObjectFactory<Object>() {
        @Override
        public Object getObject() throws BeansException {
            return new TestBean();
        }
    });
    assertSame(tb2, beanRegistry.getSingleton("tb2"));

    assertSame(tb, beanRegistry.getSingleton("tb"));
    assertSame(tb2, beanRegistry.getSingleton("tb2"));
    assertEquals(2, beanRegistry.getSingletonCount());
    assertEquals(2, beanRegistry.getSingletonNames().length);
    assertTrue(Arrays.asList(beanRegistry.getSingletonNames()).contains("tb"));
    assertTrue(Arrays.asList(beanRegistry.getSingletonNames()).contains("tb2"));

    beanRegistry.destroySingletons();
    assertEquals(0, beanRegistry.getSingletonCount());
    assertEquals(0, beanRegistry.getSingletonNames().length);
}