1 16 17 package org.springframework.context.access; 18 19 import org.springframework.beans.factory.BeanFactory; 20 import org.springframework.beans.factory.access.BeanFactoryReference; 21 import org.springframework.context.ApplicationContext; 22 import org.springframework.context.ConfigurableApplicationContext; 23 24 37 public class ContextBeanFactoryReference implements BeanFactoryReference { 38 39 private ApplicationContext applicationContext; 40 41 42 46 public ContextBeanFactoryReference(ApplicationContext applicationContext) { 47 this.applicationContext = applicationContext; 48 } 49 50 51 public BeanFactory getFactory() { 52 if (this.applicationContext == null) { 53 throw new IllegalStateException ( 54 "ApplicationContext owned by this BeanFactoryReference has been released"); 55 } 56 return this.applicationContext; 57 } 58 59 public void release() { 60 if (this.applicationContext != null) { 61 ApplicationContext savedCtx; 62 63 synchronized (this) { 65 savedCtx = this.applicationContext; 66 this.applicationContext = null; 67 } 68 69 if (savedCtx != null && savedCtx instanceof ConfigurableApplicationContext) { 70 ((ConfigurableApplicationContext) savedCtx).close(); 71 } 72 } 73 } 74 75 } 76 | Popular Tags |