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

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setScope() . 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 spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForFactoryMethod() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<String>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.setFactoryMethodName("create");
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example 2
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nonSingletonListenerInApplicationContext() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listener = new RootBeanDefinition(MyNonSingletonListener.class);
	listener.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("listener", listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	MyOtherEvent event2 = new MyOtherEvent(context);
	context.publishEvent(event2);
	MyEvent event3 = new MyEvent(context);
	context.publishEvent(event3);
	MyOtherEvent event4 = new MyOtherEvent(context);
	context.publishEvent(event4);
	assertTrue(MyNonSingletonListener.seenEvents.contains(event1));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event2));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event3));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event4));
	MyNonSingletonListener.seenEvents.clear();

	context.close();
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("test", rbd);
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		lbf.getBean("test");
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
 
Example 4
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testConstructorResourceInjectionWithNullFromFactoryBean() {
	RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NullNestedTestBeanFactoryBean.class));
	bf.registerSingleton("nestedTestBean2", new NestedTestBean());

	ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertNull(bean.getNestedTestBean());
	assertSame(bf, bean.getBeanFactory());

	bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertNull(bean.getNestedTestBean());
	assertSame(bf, bean.getBeanFactory());
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForConstructor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<String>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example 6
Source File: ConfigurationClassPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void genericsBasedInjectionWithFactoryBean() {
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("annotatedBean", bd);
	beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryFactoryBeanConfiguration.class));
	ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
	pp.postProcessBeanFactory(beanFactory);
	beanFactory.preInstantiateSingletons();

	RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) beanFactory.getBean("annotatedBean");
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.repositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.qualifiedRepositoryFactoryBean);
	assertSame(beanFactory.getBean("&repoFactoryBean"), bean.prefixQualifiedRepositoryFactoryBean);
}
 
Example 7
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLazyOptionalResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(OptionalFieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	OptionalFieldResourceInjectionBean bean = (OptionalFieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	assertNotNull(bean.getTestBeans());
	assertTrue(bean.getTestBeans().isEmpty());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 8
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testExtendedResourceInjectionWithAtRequired() {
	bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
	RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	NestedTestBean ntb = new NestedTestBean();
	bf.registerSingleton("nestedTestBean", ntb);

	TypedExtendedResourceInjectionBean bean = (TypedExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertSame(ntb, bean.getNestedTestBean());
	assertSame(bf, bean.getBeanFactory());
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithResolvedConstructorArgumentsIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("spouse"));
	lbf.registerBeanDefinition("test", rbd);
	lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	TestBean spouse = (TestBean) lbf.getBean("spouse");
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertSame(spouse, tb.getSpouse());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetBeanWithArgsNotCreatedForFactoryBeanChecking() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(ConstructorDependency.class);
	bd1.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("bd1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(ConstructorDependencyFactoryBean.class);
	bd2.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("bd2", bd2);

	ConstructorDependency bean = lbf.getBean(ConstructorDependency.class, 42);
	assertThat(bean.beanName, equalTo("bd1"));
	assertThat(bean.spouseAge, equalTo(42));

	assertEquals(1, lbf.getBeanNamesForType(ConstructorDependency.class).length);
	assertEquals(1, lbf.getBeanNamesForType(ConstructorDependencyFactoryBean.class).length);
	assertEquals(1, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)).length);
	assertEquals(0, lbf.getBeanNamesForType(ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)).length);
}
 
Example 11
Source File: AnnotationProcessorPerformanceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() {
	GenericApplicationContext ctx = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
	ctx.refresh();

	RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
	ctx.registerBeanDefinition("test", rbd);
	ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
	TestBean spouse = (TestBean) ctx.getBean("spouse");
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) ctx.getBean("test");
		assertSame(spouse, tb.getSpouse());
	}
	sw.stop();
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}
 
Example 12
Source File: FormattingConversionServiceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void formatFieldForValueInjectionUsingMetaAnnotations() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	RootBeanDefinition bd = new RootBeanDefinition(MetaValueBean.class);
	bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	ac.registerBeanDefinition("valueBean", bd);
	ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
	ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class));
	ac.refresh();
	System.setProperty("myDate", "10-31-09");
	System.setProperty("myNumber", "99.99%");
	try {
		MetaValueBean valueBean = ac.getBean(MetaValueBean.class);
		assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
		assertEquals(Double.valueOf(0.9999), valueBean.number);
	}
	finally {
		System.clearProperty("myDate");
		System.clearProperty("myNumber");
	}
}
 
Example 13
Source File: AnnotationProcessorPerformanceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() {
	GenericApplicationContext ctx = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
	ctx.refresh();

	RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
	ctx.registerBeanDefinition("test", rbd);
	ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
	TestBean spouse = (TestBean) ctx.getBean("spouse");
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) ctx.getBean("test");
		assertSame(spouse, tb.getSpouse());
	}
	sw.stop();
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 14
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void doTestLazyResourceInjection(Class<? extends TestBeanHolder> annotatedBeanClass) {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	RootBeanDefinition abd = new RootBeanDefinition(annotatedBeanClass);
	abd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	ac.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
	tbd.setLazyInit(true);
	ac.registerBeanDefinition("testBean", tbd);
	ac.refresh();

	TestBeanHolder bean = ac.getBean("annotatedBean", TestBeanHolder.class);
	assertFalse(ac.getBeanFactory().containsSingleton("testBean"));
	assertNotNull(bean.getTestBean());
	assertNull(bean.getTestBean().getName());
	assertTrue(ac.getBeanFactory().containsSingleton("testBean"));
	TestBean tb = (TestBean) ac.getBean("testBean");
	tb.setName("tb");
	assertSame("tb", bean.getTestBean().getName());
}
 
Example 15
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGenericsBasedConstructorInjectionWithMixedTargetsIncludingNonGeneric() {
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	StringRepository sr = new StringRepository();
	bf.registerSingleton("stringRepo", sr);
	SimpleRepository ngr = new SimpleRepositorySubclass();
	bf.registerSingleton("simpleRepo", ngr);

	RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean");
	assertSame(sr, bean.stringRepository);
	assertSame(ngr, bean.integerRepository);
	assertSame(1, bean.stringRepositoryArray.length);
	assertSame(1, bean.integerRepositoryArray.length);
	assertSame(sr, bean.stringRepositoryArray[0]);
	assertSame(ngr, bean.integerRepositoryArray[0]);
	assertSame(1, bean.stringRepositoryList.size());
	assertSame(1, bean.integerRepositoryList.size());
	assertSame(sr, bean.stringRepositoryList.get(0));
	assertSame(ngr, bean.integerRepositoryList.get(0));
	assertSame(1, bean.stringRepositoryMap.size());
	assertSame(1, bean.integerRepositoryMap.size());
	assertSame(sr, bean.stringRepositoryMap.get("stringRepo"));
	assertSame(ngr, bean.integerRepositoryMap.get("simpleRepo"));
}
 
Example 16
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericsBasedFieldInjectionWithSimpleMatchAndMockito() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setBeanClassName(Mockito.class.getName());
	rbd.setFactoryMethodName("mock");
	// TypedStringValue used to be equivalent to an XML-defined argument String
	rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Repository.class.getName()));
	bf.registerBeanDefinition("repo", rbd);

	RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean");
	Repository repo = bf.getBean("repo", Repository.class);
	assertSame(repo, bean.repository);
	assertSame(repo, bean.stringRepository);
	assertSame(1, bean.repositoryArray.length);
	assertSame(1, bean.stringRepositoryArray.length);
	assertSame(repo, bean.repositoryArray[0]);
	assertSame(repo, bean.stringRepositoryArray[0]);
	assertSame(1, bean.repositoryList.size());
	assertSame(1, bean.stringRepositoryList.size());
	assertSame(repo, bean.repositoryList.get(0));
	assertSame(repo, bean.stringRepositoryList.get(0));
	assertSame(1, bean.repositoryMap.size());
	assertSame(1, bean.stringRepositoryMap.size());
	assertSame(repo, bean.repositoryMap.get("repo"));
	assertSame(repo, bean.stringRepositoryMap.get("repo"));
}
 
Example 17
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericsBasedConstructorInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	StringRepository sr = new StringRepository();
	bf.registerSingleton("stringRepo", sr);
	IntegerRepository ir = new IntegerRepository();
	bf.registerSingleton("integerRepo", ir);

	RepositoryConstructorInjectionBean bean = (RepositoryConstructorInjectionBean) bf.getBean("annotatedBean");
	assertSame(sr, bean.stringRepository);
	assertSame(ir, bean.integerRepository);
	assertSame(1, bean.stringRepositoryArray.length);
	assertSame(1, bean.integerRepositoryArray.length);
	assertSame(sr, bean.stringRepositoryArray[0]);
	assertSame(ir, bean.integerRepositoryArray[0]);
	assertSame(1, bean.stringRepositoryList.size());
	assertSame(1, bean.integerRepositoryList.size());
	assertSame(sr, bean.stringRepositoryList.get(0));
	assertSame(ir, bean.integerRepositoryList.get(0));
	assertSame(1, bean.stringRepositoryMap.size());
	assertSame(1, bean.integerRepositoryMap.size());
	assertSame(sr, bean.stringRepositoryMap.get("stringRepo"));
	assertSame(ir, bean.integerRepositoryMap.get("integerRepo"));
}
 
Example 18
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourceInjectionWithPrototypes() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setResourceFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd1 = new RootBeanDefinition(TestBean.class);
	tbd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean", tbd1);
	RootBeanDefinition tbd2 = new RootBeanDefinition(TestBean.class);
	tbd2.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean2", tbd2);

	ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertTrue(bean.init3Called);

	TestBean tb = bean.getTestBean();
	TestBean tb2 = bean.getTestBean2();
	assertNotNull(tb);
	assertNotNull(tb2);

	ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean(), tb);
	assertNotSame(anotherBean.getTestBean2(), tb2);

	bf.destroyBean("annotatedBean", bean);
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
	assertTrue(bean.destroy3Called);
}
 
Example 19
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGenericsBasedFactoryBeanInjectionWithBeanDefinition() {
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	bf.registerBeanDefinition("repoFactoryBean", new RootBeanDefinition(RepositoryFactoryBean.class));

	RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) bf.getBean("annotatedBean");
	RepositoryFactoryBean<?> repoFactoryBean = bf.getBean("&repoFactoryBean", RepositoryFactoryBean.class);
	assertSame(repoFactoryBean, bean.repositoryFactoryBean);
}
 
Example 20
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private RootBeanDefinition createConstructorDependencyBeanDefinition(int age) {
	RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependency.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.getConstructorArgumentValues().addGenericArgumentValue(age);
	return bd;
}