1 16 17 package org.springframework.context.support; 18 19 import java.io.IOException ; 20 21 import org.springframework.beans.BeansException; 22 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 23 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 24 import org.springframework.context.ApplicationContext; 25 import org.springframework.context.ApplicationContextException; 26 27 60 public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext { 61 62 63 private DefaultListableBeanFactory beanFactory; 64 65 66 private final Object beanFactoryMonitor = new Object (); 67 68 69 72 public AbstractRefreshableApplicationContext() { 73 } 74 75 79 public AbstractRefreshableApplicationContext(ApplicationContext parent) { 80 super(parent); 81 } 82 83 84 protected final void refreshBeanFactory() throws BeansException { 85 ConfigurableListableBeanFactory oldBeanFactory = null; 87 synchronized (this.beanFactoryMonitor) { 88 oldBeanFactory = this.beanFactory; 89 } 90 if (oldBeanFactory != null) { 91 oldBeanFactory.destroySingletons(); 92 synchronized (this.beanFactoryMonitor) { 93 this.beanFactory = null; 94 } 95 } 96 97 try { 99 DefaultListableBeanFactory beanFactory = createBeanFactory(); 100 loadBeanDefinitions(beanFactory); 101 synchronized (this.beanFactoryMonitor) { 102 this.beanFactory = beanFactory; 103 } 104 } 105 catch (IOException ex) { 106 throw new ApplicationContextException( 107 "I/O error parsing XML document for application context [" + getDisplayName() + "]", ex); 108 } 109 } 110 111 protected final void closeBeanFactory() { 112 synchronized (this.beanFactoryMonitor) { 113 this.beanFactory = null; 114 } 115 } 116 117 public final ConfigurableListableBeanFactory getBeanFactory() { 118 synchronized (this.beanFactoryMonitor) { 119 if (this.beanFactory == null) { 120 throw new IllegalStateException ("BeanFactory not initialized or already closed - " + 121 "call 'refresh' before accessing beans via the ApplicationContext"); 122 } 123 return this.beanFactory; 124 } 125 } 126 127 128 142 protected DefaultListableBeanFactory createBeanFactory() { 143 return new DefaultListableBeanFactory(getInternalParentBeanFactory()); 144 } 145 146 157 protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { 158 } 159 160 169 protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) 170 throws IOException , BeansException; 171 172 } 173 | Popular Tags |