/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
/** * Create an internal bean factory for this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation creates a * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory} * with the {@linkplain #getInternalParentBeanFactory() internal bean factory} of this * context's parent as parent bean factory. Can be overridden in subclasses, * for example to customize DefaultListableBeanFactory's settings. * @return the bean factory for this context * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping */ protected DefaultListableBeanFactory createBeanFactory() { return new DefaultListableBeanFactory(getInternalParentBeanFactory()); }
ctx.stop(); ctx.close(); ctx.setClassLoader(OpenmrsClassLoader.getInstance()); Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); ctx.refresh(); ctx.setClassLoader(OpenmrsClassLoader.getInstance()); Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance());
public ApplicationContext createApplicatonContext(FlowDefinition processDefinition, Map<String, ? extends Object> projectProperties, final ApplicationContext parent) { Resource[] configResources = null; ClassLoader classLoader = this.getClass().getClassLoader(); ClassLoader originalClassLoader = ClassUtils.overrideThreadContextClassLoader(classLoader); try { Assert.notNull(processDefinition); Assert.notNull(processDefinition.getProcessingConfiguration()); Resource processContext = new InputStreamResource(processDefinition.getProcessingConfiguration()); configResources = createResources(processContext, originalClassLoader); } finally { if (originalClassLoader != null) { Thread.currentThread().setContextClassLoader(originalClassLoader); } } ProcessResourceLocator processResourceLocator = new ProcessResourceLocator(processDefinition); externalResourceLocators.add(processResourceLocator); Properties properties = convertProperties(projectProperties); AbstractRefreshableApplicationContext applicationContext = new ProcessXmlApplicationContext(configResources, externalResourceLocators); // BeanPostProcessor applicationContext.setParent(parent); for (BeanFactoryPostProcessor postProcessor : postProcessors) { if (postProcessor instanceof PropertiesLoaderSupport) { ((PropertiesLoaderSupport) postProcessor).setProperties(properties); } applicationContext.addBeanFactoryPostProcessor(postProcessor); } applicationContext.refresh(); return applicationContext; }
public class ContextChecker { public static void main( String[] args ) { AbstractRefreshableApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath*:/beans.xml" ); ctx.setAllowCircularReferences( false ); ctx.refresh(); } }
@Override public void setApplicationContext(ApplicationContext applicationContext) { context = applicationContext; if (applicationContext instanceof AbstractRefreshableApplicationContext) { AbstractRefreshableApplicationContext springContext = (AbstractRefreshableApplicationContext) applicationContext; springFactory = (DefaultListableBeanFactory) springContext.getBeanFactory(); } else if (applicationContext instanceof GenericApplicationContext) { GenericApplicationContext springContext = (GenericApplicationContext) applicationContext; springFactory = springContext.getDefaultListableBeanFactory(); } }
protected void destroyBeans() { super.destroyBeans(); try { cleanOsgiBundleScope(getBeanFactory()); } catch (Exception ex) { logger.warn("got exception when closing", ex); } }
/** * Sets a default config location if no explicit config location specified. * @see #getDefaultConfigLocations * @see #setConfigLocations */ public void refresh() throws BeansException { if (this.configLocations == null || this.configLocations.length == 0) { setConfigLocations(getDefaultConfigLocations()); } super.refresh(); }
@Override public void setApplicationContext(ApplicationContext applicationContext) { context = applicationContext; if (applicationContext instanceof AbstractRefreshableApplicationContext) { AbstractRefreshableApplicationContext springContext = (AbstractRefreshableApplicationContext) applicationContext; springFactory = (DefaultListableBeanFactory) springContext.getBeanFactory(); } else if (applicationContext instanceof GenericApplicationContext) { GenericApplicationContext springContext = (GenericApplicationContext) applicationContext; springFactory = springContext.getDefaultListableBeanFactory(); } }
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
/** * Use the application context to resolve any embedded property values within the original * resource. */ @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { try (InputStream is = originalResource.getInputStream();) { ConfigurableListableBeanFactory beanFactory = ((AbstractRefreshableApplicationContext) appContext) .getBeanFactory(); StringBuilder sb = new StringBuilder(); Iterator<String> iter = IOUtils.lineIterator(is, "UTF-8"); boolean transformed = false; while (iter.hasNext()) { String line = iter.next(); if (line.contains("${")) { transformed = true; line = beanFactory.resolveEmbeddedValue(line); } sb.append(line); } transformedResource = !transformed ? originalResource : new ByteArrayResource(sb.toString().getBytes()); } catch (IOException e) { throw MiscUtil.toUnchecked(e); } }
/** * Create an internal bean factory for this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation creates a * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory} * with the {@linkplain #getInternalParentBeanFactory() internal bean factory} of this * context's parent as parent bean factory. Can be overridden in subclasses, * for example to customize DefaultListableBeanFactory's settings. * @return the bean factory for this context * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping */ protected DefaultListableBeanFactory createBeanFactory() { return new DefaultListableBeanFactory(getInternalParentBeanFactory()); }
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
public void setConfig(String config) { this.config = config; logger.debug("Loading configuration from [" + config + "]."); try { applicationContext = new FileSystemXmlApplicationContext( new String[] { "file:" + config }, false); applicationContext.setClassLoader(getClass().getClassLoader()); applicationContext.refresh(); this.applicationContext.getBeanFactory().autowireBeanProperties( this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, isDependencyCheck()); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } }
/** * Create an internal bean factory for this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation creates a * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory} * with the {@linkplain #getInternalParentBeanFactory() internal bean factory} of this * context's parent as parent bean factory. Can be overridden in subclasses, * for example to customize DefaultListableBeanFactory's settings. * @return the bean factory for this context * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping */ protected DefaultListableBeanFactory createBeanFactory() { return new DefaultListableBeanFactory(getInternalParentBeanFactory()); }