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

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setInitMethodName() . 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
@SuppressWarnings("unchecked")
public void testInitSecurityAwarePrototypeBean() {
	final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
	bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
	bd.setInitMethodName("init");
	lbf.registerBeanDefinition("test", bd);
	final Subject subject = new Subject();
	subject.getPrincipals().add(new TestPrincipal("user1"));

	TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject,
			(PrivilegedAction) () -> lbf.getBean("test"), null);
	assertNotNull(bean);
	assertEquals("user1", bean.getUserName());
}
 
Example 3
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 4
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testInitSecurityAwarePrototypeBean() {
	final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
	bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
	bd.setInitMethodName("init");
	lbf.registerBeanDefinition("test", bd);
	final Subject subject = new Subject();
	subject.getPrincipals().add(new TestPrincipal("user1"));

	TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject,
			new PrivilegedAction() {
				@Override
				public Object run() {
					return lbf.getBean("test");
				}
			}, null);
	assertNotNull(bean);
	assertEquals("user1", bean.getUserName());
}
 
Example 5
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 6
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testInitSecurityAwarePrototypeBean() {
	final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
	bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
	bd.setInitMethodName("init");
	lbf.registerBeanDefinition("test", bd);
	final Subject subject = new Subject();
	subject.getPrincipals().add(new TestPrincipal("user1"));

	TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject,
			new PrivilegedAction() {
				@Override
				public Object run() {
					return lbf.getBean("test");
				}
			}, null);
	assertNotNull(bean);
	assertEquals("user1", bean.getUserName());
}
 
Example 7
Source File: Spr3775InitDestroyLifecycleTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private DefaultListableBeanFactory createBeanFactoryAndRegisterBean(final Class<?> beanClass,
		final String initMethodName, final String destroyMethodName) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
	beanDefinition.setInitMethodName(initMethodName);
	beanDefinition.setDestroyMethodName(destroyMethodName);
	beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	beanFactory.registerBeanDefinition(LIFECYCLE_TEST_BEAN, beanDefinition);
	return beanFactory;
}
 
Example 8
Source File: Spr3775InitDestroyLifecycleTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private DefaultListableBeanFactory createBeanFactoryAndRegisterBean(final Class<?> beanClass,
		final String initMethodName, final String destroyMethodName) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
	beanDefinition.setInitMethodName(initMethodName);
	beanDefinition.setDestroyMethodName(destroyMethodName);
	beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	beanFactory.registerBeanDefinition(LIFECYCLE_TEST_BEAN, beanDefinition);
	return beanFactory;
}
 
Example 9
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);
}
 
Example 10
Source File: Spr3775InitDestroyLifecycleTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private DefaultListableBeanFactory createBeanFactoryAndRegisterBean(final Class<?> beanClass,
		final String initMethodName, final String destroyMethodName) {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
	beanDefinition.setInitMethodName(initMethodName);
	beanDefinition.setDestroyMethodName(destroyMethodName);
	beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	beanFactory.registerBeanDefinition(LIFECYCLE_TEST_BEAN, beanDefinition);
	return beanFactory;
}