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.BeanDefinition; 23 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 24 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 25 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 26 import org.springframework.context.ApplicationContext; 27 import org.springframework.core.io.Resource; 28 import org.springframework.core.io.ResourceLoader; 29 import org.springframework.core.io.support.ResourcePatternResolver; 30 import org.springframework.util.Assert; 31 32 83 public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry { 84 85 private final DefaultListableBeanFactory beanFactory; 86 87 private ResourceLoader resourceLoader; 88 89 private boolean refreshed = false; 90 91 92 97 public GenericApplicationContext() { 98 this.beanFactory = new DefaultListableBeanFactory(); 99 } 100 101 107 public GenericApplicationContext(DefaultListableBeanFactory beanFactory) { 108 Assert.notNull(beanFactory, "BeanFactory must not be null"); 109 this.beanFactory = beanFactory; 110 } 111 112 118 public GenericApplicationContext(ApplicationContext parent) { 119 this(); 120 setParent(parent); 121 } 122 123 130 public GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent) { 131 this(beanFactory); 132 setParent(parent); 133 } 134 135 136 141 public void setParent(ApplicationContext parent) { 142 super.setParent(parent); 143 this.beanFactory.setParentBeanFactory(getInternalParentBeanFactory()); 144 } 145 146 164 public void setResourceLoader(ResourceLoader resourceLoader) { 165 this.resourceLoader = resourceLoader; 166 } 167 168 169 174 public Resource getResource(String location) { 175 if (this.resourceLoader != null) { 176 return this.resourceLoader.getResource(location); 177 } 178 return super.getResource(location); 179 } 180 181 187 public Resource[] getResources(String locationPattern) throws IOException { 188 if (this.resourceLoader instanceof ResourcePatternResolver) { 189 return ((ResourcePatternResolver) this.resourceLoader).getResources(locationPattern); 190 } 191 return super.getResources(locationPattern); 192 } 193 194 195 199 204 protected final void refreshBeanFactory() throws IllegalStateException { 205 if (this.refreshed) { 206 throw new IllegalStateException ( 207 "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once"); 208 } 209 this.refreshed = true; 210 } 211 212 216 protected final void closeBeanFactory() { 217 } 218 219 223 public final ConfigurableListableBeanFactory getBeanFactory() { 224 return this.beanFactory; 225 } 226 227 235 public final DefaultListableBeanFactory getDefaultListableBeanFactory() { 236 return this.beanFactory; 237 } 238 239 240 244 public BeanDefinition getBeanDefinition(String beanName) throws BeansException { 245 return this.beanFactory.getBeanDefinition(beanName); 246 } 247 248 public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeansException { 249 this.beanFactory.registerBeanDefinition(beanName, beanDefinition); 250 } 251 252 public void registerAlias(String beanName, String alias) throws BeansException { 253 this.beanFactory.registerAlias(beanName, alias); 254 } 255 256 } 257 | Popular Tags |