/** * Set whether it should be allowed to override bean definitions by registering * a different definition with the same name, automatically replacing the former. * If not, an exception will be thrown. Default is "true". * @since 3.0 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding */ public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { this.beanFactory.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); }
/** * Set whether it should be allowed to override bean definitions by registering * a different definition with the same name, automatically replacing the former. * If not, an exception will be thrown. Default is "true". * @since 3.0 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding */ public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { this.beanFactory.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); }
/** * Customize the internal bean factory used by this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation applies this context's * {@linkplain #setAllowBeanDefinitionOverriding "allowBeanDefinitionOverriding"} * and {@linkplain #setAllowCircularReferences "allowCircularReferences"} settings, * if specified. Can be overridden in subclasses to customize any of * {@link DefaultListableBeanFactory}'s settings. * @param beanFactory the newly created bean factory for this context * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see DefaultListableBeanFactory#setAllowCircularReferences * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping * @see DefaultListableBeanFactory#setAllowEagerClassLoading */ protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { if (this.allowBeanDefinitionOverriding != null) { beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.allowCircularReferences != null) { beanFactory.setAllowCircularReferences(this.allowCircularReferences); } }
/** * Customize the internal bean factory used by this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation applies this context's * {@linkplain #setAllowBeanDefinitionOverriding "allowBeanDefinitionOverriding"} * and {@linkplain #setAllowCircularReferences "allowCircularReferences"} settings, * if specified. Can be overridden in subclasses to customize any of * {@link DefaultListableBeanFactory}'s settings. * @param beanFactory the newly created bean factory for this context * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see DefaultListableBeanFactory#setAllowCircularReferences * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping * @see DefaultListableBeanFactory#setAllowEagerClassLoading */ protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { if (this.allowBeanDefinitionOverriding != null) { beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.allowCircularReferences != null) { beanFactory.setAllowCircularReferences(this.allowCircularReferences); } }
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { beanFactory.registerSingleton("springBootBanner", printedBanner); } if (beanFactory instanceof DefaultListableBeanFactory) { ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } // Load the sources Set<Object> sources = getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); load(context, sources.toArray(new Object[0])); listeners.contextLoaded(context); }
@Test public void postProcessorFailsOnImplicitOverrideIfOverridingIsNotAllowed() { RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class); rbd.setResource(new DescriptiveResource("XML or something")); beanFactory.registerBeanDefinition("bar", rbd); beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class)); beanFactory.setAllowBeanDefinitionOverriding(false); ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor(); try { pp.postProcessBeanFactory(beanFactory); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { assertTrue(ex.getMessage().contains("bar")); assertTrue(ex.getMessage().contains("SingletonBeanConfig")); assertTrue(ex.getMessage().contains(TestBean.class.getName())); } }
@Test public void testBeanDefinitionOverridingNotAllowed() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setAllowBeanDefinitionOverriding(false); BeanDefinition oldDef = new RootBeanDefinition(TestBean.class); BeanDefinition newDef = new RootBeanDefinition(NestedTestBean.class); lbf.registerBeanDefinition("test", oldDef); try { lbf.registerBeanDefinition("test", newDef); fail("Should have thrown BeanDefinitionOverrideException"); } catch (BeanDefinitionOverrideException ex) { assertEquals("test", ex.getBeanName()); assertSame(newDef, ex.getBeanDefinition()); assertSame(oldDef, ex.getExistingDefinition()); } }
@Test public void testBeanDefinitionRemoval() { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); lbf.setAllowBeanDefinitionOverriding(false); lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); lbf.registerAlias("test", "test2"); lbf.preInstantiateSingletons(); lbf.removeBeanDefinition("test"); lbf.removeAlias("test2"); lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); lbf.registerAlias("test", "test2"); assertTrue(lbf.getBean("test") instanceof NestedTestBean); assertTrue(lbf.getBean("test2") instanceof NestedTestBean); }
@Override protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { beanFactory.setAllowBeanDefinitionOverriding(false); }
@Bean public static BeanFactoryPostProcessor beanFactoryPostProcessor() { return beanFactory -> ((DefaultListableBeanFactory) beanFactory).setAllowBeanDefinitionOverriding(false); }
/** * The application context will call this to set the override behavior of the bean factory. We * intercept this to set this as the default behavior for the bean factory. This setting is used * when no cwf:override attribute is specified. */ @Override public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { super.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); this.defaultOverriding = allowBeanDefinitionOverriding; }
/** * The application context will call this to set the override behavior of the bean factory. We * intercept this to set this as the default behavior for the bean factory. This setting is used * when no cwf:override attribute is specified. */ @Override public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { super.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); this.defaultOverriding = allowBeanDefinitionOverriding; }
/** * Set whether it should be allowed to override bean definitions by registering * a different definition with the same name, automatically replacing the former. * If not, an exception will be thrown. Default is "true". * @since 3.0 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding */ public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) { this.beanFactory.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); }
@Override protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { super.customizeBeanFactory(beanFactory); beanFactory.setAllowBeanDefinitionOverriding(false); } };
@Override protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { super.customizeBeanFactory(beanFactory); beanFactory.setAllowBeanDefinitionOverriding(false); } };
/** * Customize the internal bean factory used by this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation applies this context's * {@linkplain #setAllowBeanDefinitionOverriding "allowBeanDefinitionOverriding"} * and {@linkplain #setAllowCircularReferences "allowCircularReferences"} settings, * if specified. Can be overridden in subclasses to customize any of * {@link DefaultListableBeanFactory}'s settings. * @param beanFactory the newly created bean factory for this context * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see DefaultListableBeanFactory#setAllowCircularReferences * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping * @see DefaultListableBeanFactory#setAllowEagerClassLoading */ protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { if (this.allowBeanDefinitionOverriding != null) { beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.allowCircularReferences != null) { beanFactory.setAllowCircularReferences(this.allowCircularReferences); } }
super.setAllowBeanDefinitionOverriding(canOverride); super.registerBeanDefinition(beanName, beanDefinition);
public BundleRegistry(){ super(); getBeanFactory().registerScope(BUNDLE_SCOPE, this); getBeanFactory().registerScope(EMPTY_SCOPE, new EmptyScope()); getDefaultListableBeanFactory().setAllowBeanDefinitionOverriding(true); }
private void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(getParent() != null ? getParent().getBeanFactory() : null); beanFactory.setAllowBeanDefinitionOverriding(true); beanFactory.setAllowCircularReferences(true); beanFactory.setAllowEagerClassLoading(false); loadBeanDefinitions(beanFactory); synchronized (beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException e) { throw new BeanContainerContextException("I/O error parsing XML document for application context: " + this, e); } }
private void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(getParent() != null ? getParent().getBeanFactory() : null); beanFactory.setAllowBeanDefinitionOverriding(true); beanFactory.setAllowCircularReferences(true); beanFactory.setAllowEagerClassLoading(false); loadBeanDefinitions(beanFactory); synchronized (beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException e) { throw new BeanContainerContextException("I/O error parsing XML document for application context: " + this, e); } }