org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding ( )源码实例Demo

下面列出了org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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 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);
}
 
@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);
}
 
源代码5 项目: DataLink   文件: ApplicationContextBootStrap.java
public void boot() {
    try {
        applicationContext = new ClassPathXmlApplicationContext(filePath) {

            @Override
            protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
                super.customizeBeanFactory(beanFactory);
                beanFactory.setAllowBeanDefinitionOverriding(false);
            }
        };
    } catch (Throwable e) {
        throw new DatalinkException("ERROR ## Datalink Factory initial failed.", e);
    }
}
 
@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);
}
 
@Test
public void testBeanDefinitionOverridingNotAllowed() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setAllowBeanDefinitionOverriding(false);
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	try {
		lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		assertEquals("test", ex.getBeanName());
		// expected
	}
}
 
public TaskScheduleCommandTemplate(JLineShellComponent dataFlowShell, ApplicationContext applicationContext) {
	this.dataFlowShell = dataFlowShell;

	ConfigurableListableBeanFactory beanFactory = ((AnnotationConfigServletWebServerApplicationContext) applicationContext)
			.getBeanFactory();
	schedule = Mockito.mock(SchedulerService.class);
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TaskSchedulerController.class);
	builder.addConstructorArgValue(schedule);
	DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;
	listableBeanFactory.setAllowBeanDefinitionOverriding(true);
	listableBeanFactory.registerBeanDefinition("taskSchedulerController", builder.getBeanDefinition());
}
 
@Before
public void init() {
    registry = new DefaultListableBeanFactory();
    registry.setAllowBeanDefinitionOverriding(false);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(registry);
}
 
/**
 * 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) {
		// 默认是 false,不允许覆盖
		beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
	}
	if (this.allowCircularReferences != null) {
		// 默认是 false,不允许循环引用
		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);
	}
}
 
/**
 * 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);
	}
}