ServiceBeanInstance implements StatefulAstrixBean, InvocationHandler { void destroy() { log.info("Destroying service bean. bean={} astrixBeanId={}", getBeanKey(), id); beanStateLock.lock(); try { this.currentState.releaseInstance(); } finally { beanStateLock.unlock(); } } private  ServiceBeanInstance(ServiceDefinition<T> serviceDefinition, 
								AstrixBeanKey<T> beanKey, 
								ServiceDiscovery serviceDiscovery, 
								ServiceComponentRegistry serviceComponents,
								ServiceBeanProxies beanProxies,
								ReactiveTypeConverter reactiveTypeConverter, DynamicBooleanProperty available); static ServiceBeanInstance<T> create(ServiceDefinition<T> serviceDefinition, 
													AstrixBeanKey<T> beanKey, 
													ServiceDiscovery serviceDiscovery, 
													ServiceBeanContext serviceBeanContext); void renewLease(); void bind(); @Override void waitUntilBound(long timeoutMillis); boolean isBound(); AstrixBeanKey<T> getBeanKey(); @Override Object invoke(Object proxy, Method method, Object[] args);  }
@Test public void boundServiceInstancesShouldBeReleasedWhenContextIsDestroyed() throws Exception { InMemoryServiceRegistry serviceRegistry = new InMemoryServiceRegistry(); serviceRegistry.registerProvider(Ping.class, new PingImpl()); TestAstrixConfigurer astrixConfigurer = new TestAstrixConfigurer(); astrixConfigurer.registerApiProvider(PingApiProvider.class); astrixConfigurer.set(AstrixSettings.SERVICE_REGISTRY_URI, serviceRegistry.getServiceUri()); AstrixApplicationContext astrixContext = (AstrixApplicationContext) astrixConfigurer.configure(); Ping ping = astrixContext.getBean(Ping.class); assertEquals("foo", ping.ping("foo")); DirectComponent directComponent = (DirectComponent) astrixContext.getInstance(ServiceComponentRegistry.class).getComponent(AstrixServiceComponentNames.DIRECT); assertEquals(2, directComponent.getBoundServices().size()); assertThat("Expected at least one service to be bound after pingBean is bound", directComponent.getBoundServices().size(), greaterThanOrEqualTo(1)); astrixContext.destroy(); assertEquals("All bound beans should be release when context is destroyed", 0, directComponent.getBoundServices().size()); }