Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setDependsOn()

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setDependsOn() . 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: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb2", bd2);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb2'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testImplicitDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb3");
	lbf.registerBeanDefinition("tb2", bd2);
	RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class);
	bd3.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb3", bd3);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb3'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
Example 4
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb2", bd2);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb2'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testImplicitDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb3");
	lbf.registerBeanDefinition("tb2", bd2);
	RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class);
	bd3.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb3", bd3);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb3'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
Example 7
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb2", bd2);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb2'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testImplicitDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb3");
	lbf.registerBeanDefinition("tb2", bd2);
	RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class);
	bd3.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb3", bd3);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb3'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
Example 10
Source File: DataSourceBeanFactoryPostProcessor.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
private void createDataSourceProxy(ConfigurableListableBeanFactory beanFactory,
                                   String beanName, BeanDefinition originDataSource,
                                   String jdbcUrl) {
    // re-register origin datasource bean
    BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
    beanDefinitionRegistry.removeBeanDefinition(beanName);
    boolean isPrimary = originDataSource.isPrimary();
    originDataSource.setPrimary(false);
    beanDefinitionRegistry.registerBeanDefinition(transformDatasourceBeanName(beanName),
        originDataSource);
    // register proxied datasource
    RootBeanDefinition proxiedBeanDefinition = new RootBeanDefinition(SmartDataSource.class);
    proxiedBeanDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
    proxiedBeanDefinition.setPrimary(isPrimary);
    proxiedBeanDefinition.setInitMethodName("init");
    proxiedBeanDefinition.setDependsOn(transformDatasourceBeanName(beanName));
    MutablePropertyValues originValues = originDataSource.getPropertyValues();
    MutablePropertyValues values = new MutablePropertyValues();
    String appName = environment.getProperty(TRACER_APPNAME_KEY);
    Assert.isTrue(!StringUtils.isBlank(appName), TRACER_APPNAME_KEY + " must be configured!");
    values.add("appName", appName);
    values.add("delegate", new RuntimeBeanReference(transformDatasourceBeanName(beanName)));
    values.add("dbType",
        DataSourceUtils.resolveDbTypeFromUrl(unwrapPropertyValue(originValues.get(jdbcUrl))));
    values.add("database",
        DataSourceUtils.resolveDatabaseFromUrl(unwrapPropertyValue(originValues.get(jdbcUrl))));
    proxiedBeanDefinition.setPropertyValues(values);
    beanDefinitionRegistry.registerBeanDefinition(beanName, proxiedBeanDefinition);
}