Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
	}
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
	}
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 5
Source File: ApplicationContextBootStrap.java    From DataLink with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
	}
}
 
Example 8
Source File: TaskScheduleCommandTemplate.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 9
Source File: AnnotatedBeanDefinitionRegistryUtilsTest.java    From spring-context-support with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
    registry = new DefaultListableBeanFactory();
    registry.setAllowBeanDefinitionOverriding(false);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(registry);
}
 
Example 10
Source File: AbstractRefreshableApplicationContext.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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);
	}
}
 
Example 11
Source File: AbstractRefreshableApplicationContext.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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);
	}
}
 
Example 12
Source File: AbstractRefreshableApplicationContext.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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);
	}
}
 
Example 13
Source File: AbstractRefreshableApplicationContext.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
	}
}