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

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setFactoryMethodName() . 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 testStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.setFactoryMethodName("createTestBean");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.setFactoryMethodName("createTestBean");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 3
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore  // SPR-11521
public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatchAgainstFactoryMethodSignature() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(GenericInterface1Impl.class);
	bd.setFactoryMethodName("createErased");
	bf.registerBeanDefinition("bean1", bd);
	bf.registerBeanDefinition("bean2", new RootBeanDefinition(GenericInterface2Impl.class));
	bf.registerBeanDefinition("bean2a", new RootBeanDefinition(ReallyGenericInterface2Impl.class));
	bf.registerBeanDefinition("bean2b", new RootBeanDefinition(PlainGenericInterface2Impl.class));

	GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1");
	GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2");
	assertSame(bean2, bean1.gi2);
}
 
Example 4
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericsBasedInjectionIntoUnresolvedTypeVariable() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(GenericInterface1Impl.class);
	bd.setFactoryMethodName("createPlain");
	bf.registerBeanDefinition("bean1", bd);
	bf.registerBeanDefinition("bean2", new RootBeanDefinition(GenericInterface2Impl.class));

	GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1");
	GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2");
	assertSame(bean2, bean1.gi2);
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryMethodName("createTestBean");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 6
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 7
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testObjectProviderInjectionWithTargetPrimary() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class));
	RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class);
	tb1.setFactoryMethodName("newTestBean1");
	tb1.setPrimary(true);
	bf.registerBeanDefinition("testBean1", tb1);
	RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class);
	tb2.setFactoryMethodName("newTestBean2");
	tb2.setLazyInit(true);
	bf.registerBeanDefinition("testBean2", tb2);

	ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean");
	assertSame(bf.getBean("testBean1"), bean.getTestBean());
	assertSame(bf.getBean("testBean1"), bean.getOptionalTestBean());
	assertSame(bf.getBean("testBean1"), bean.consumeOptionalTestBean());
	assertSame(bf.getBean("testBean1"), bean.getUniqueTestBean());
	assertSame(bf.getBean("testBean1"), bean.consumeUniqueTestBean());
	assertFalse(bf.containsSingleton("testBean2"));

	List<?> testBeans = bean.iterateTestBeans();
	assertEquals(2, testBeans.size());
	assertSame(bf.getBean("testBean1"), testBeans.get(0));
	assertSame(bf.getBean("testBean2"), testBeans.get(1));
	testBeans = bean.forEachTestBeans();
	assertEquals(2, testBeans.size());
	assertSame(bf.getBean("testBean1"), testBeans.get(0));
	assertSame(bf.getBean("testBean2"), testBeans.get(1));
	testBeans = bean.streamTestBeans();
	assertEquals(2, testBeans.size());
	assertSame(bf.getBean("testBean1"), testBeans.get(0));
	assertSame(bf.getBean("testBean2"), testBeans.get(1));
	testBeans = bean.sortedTestBeans();
	assertEquals(2, testBeans.size());
	assertSame(bf.getBean("testBean2"), testBeans.get(0));
	assertSame(bf.getBean("testBean1"), testBeans.get(1));
}
 
Example 8
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 9
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void populatedJavaUtilOptionalBean() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(Optional.class);
	bd.setFactoryMethodName("of");
	bd.getConstructorArgumentValues().addGenericArgumentValue("CONTENT");
	bf.registerBeanDefinition("optionalBean", bd);
	assertEquals(Optional.of("CONTENT"), bf.getBean(Optional.class));
}
 
Example 10
Source File: Spr5475Tests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void noArgFactoryMethodInvokedWithTwoArgsAndTypesSpecified() {
	RootBeanDefinition def = new RootBeanDefinition(Foo.class);
	def.setFactoryMethodName("noArgFactory");
	ConstructorArgumentValues cav = new ConstructorArgumentValues();
	cav.addIndexedArgumentValue(0, "bogusArg1", CharSequence.class.getName());
	cav.addIndexedArgumentValue(1, "bogusArg2".getBytes());
	def.setConstructorArgumentValues(cav);

	assertExceptionMessageForMisconfiguredFactoryMethod(
			def,
			"Error creating bean with name 'foo': No matching factory method found: factory method 'noArgFactory(CharSequence,byte[])'. " +
			"Check that a method with the specified name and arguments exists and that it is static.");
}
 
Example 11
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGenericsBasedFieldInjectionWithSimpleMatchAndMockito() {
	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 12
Source File: SpringConfiguredBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
	if (!parserContext.getRegistry().containsBeanDefinition(BEAN_CONFIGURER_ASPECT_BEAN_NAME)) {
		RootBeanDefinition def = new RootBeanDefinition();
		def.setBeanClassName(BEAN_CONFIGURER_ASPECT_CLASS_NAME);
		def.setFactoryMethodName("aspectOf");
		def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		def.setSource(parserContext.extractSource(element));
		parserContext.registerBeanComponent(new BeanComponentDefinition(def, BEAN_CONFIGURER_ASPECT_BEAN_NAME));
	}
	return null;
}
 
Example 13
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("rawtypes")
public void testGenericsBasedInjectionIntoUnresolvedTypeVariable() {
	RootBeanDefinition bd = new RootBeanDefinition(GenericInterface1Impl.class);
	bd.setFactoryMethodName("createPlain");
	bf.registerBeanDefinition("bean1", bd);
	bf.registerBeanDefinition("bean2", new RootBeanDefinition(GenericInterface2Impl.class));

	GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1");
	GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2");
	assertSame(bean2, bean1.gi2);
	assertEquals(ResolvableType.forClass(GenericInterface1Impl.class), bd.getResolvableType());
}
 
Example 14
Source File: AnnotationDrivenCacheBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static void registerCacheAspect(Element element, ParserContext parserContext) {
	if (!parserContext.getRegistry().containsBeanDefinition(CacheManagementConfigUtils.JCACHE_ASPECT_BEAN_NAME)) {
		Object eleSource = parserContext.extractSource(element);
		RootBeanDefinition def = new RootBeanDefinition();
		def.setBeanClassName(JCACHE_ASPECT_CLASS_NAME);
		def.setFactoryMethodName("aspectOf");
		BeanDefinition sourceDef = createJCacheOperationSourceBeanDefinition(element, eleSource);
		String sourceName =
				parserContext.getReaderContext().registerWithGeneratedName(sourceDef);
		def.getPropertyValues().add("cacheOperationSource", new RuntimeBeanReference(sourceName));

		parserContext.registerBeanComponent(new BeanComponentDefinition(sourceDef, sourceName));
		parserContext.registerBeanComponent(new BeanComponentDefinition(def, CacheManagementConfigUtils.JCACHE_ASPECT_BEAN_NAME));
	}
}
 
Example 15
Source File: AnnotationDrivenBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void registerTransactionAspect(Element element, ParserContext parserContext) {
	String txAspectBeanName = TransactionManagementConfigUtils.TRANSACTION_ASPECT_BEAN_NAME;
	String txAspectClassName = TransactionManagementConfigUtils.TRANSACTION_ASPECT_CLASS_NAME;
	if (!parserContext.getRegistry().containsBeanDefinition(txAspectBeanName)) {
		RootBeanDefinition def = new RootBeanDefinition();
		def.setBeanClassName(txAspectClassName);
		def.setFactoryMethodName("aspectOf");
		registerTransactionManager(element, def);
		parserContext.registerBeanComponent(new BeanComponentDefinition(def, txAspectBeanName));
	}
}
 
Example 16
Source File: SpringConfiguredBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
	if (!parserContext.getRegistry().containsBeanDefinition(BEAN_CONFIGURER_ASPECT_BEAN_NAME)) {
		RootBeanDefinition def = new RootBeanDefinition();
		def.setBeanClassName(BEAN_CONFIGURER_ASPECT_CLASS_NAME);
		def.setFactoryMethodName("aspectOf");
		def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		def.setSource(parserContext.extractSource(element));
		parserContext.registerBeanComponent(new BeanComponentDefinition(def, BEAN_CONFIGURER_ASPECT_BEAN_NAME));
	}
	return null;
}
 
Example 17
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithFactoryBean() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class);
	beanDef.setFactoryBeanName("testBeanFactory");
	beanDef.setFactoryMethodName("create");
	factory.registerBeanDefinition("testBean", beanDef);
	factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class));
	RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(factory);
	factory.addBeanPostProcessor(bpp);
	factory.preInstantiateSingletons();
}
 
Example 18
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testConstructorResourceInjectionWithNullFromFactoryMethod() {
	RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	RootBeanDefinition tb = new RootBeanDefinition(NullFactoryMethods.class);
	tb.setFactoryMethodName("createTestBean");
	bf.registerBeanDefinition("testBean", tb);
	RootBeanDefinition ntb = new RootBeanDefinition(NullFactoryMethods.class);
	ntb.setFactoryMethodName("createNestedTestBean");
	bf.registerBeanDefinition("nestedTestBean", ntb);
	bf.registerSingleton("nestedTestBean2", new NestedTestBean());

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

	bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
	assertNull(bean.getTestBean());
	assertNull(bean.getTestBean2());
	assertNull(bean.getTestBean3());
	assertNull(bean.getTestBean4());
	assertNull(bean.getNestedTestBean());
	assertSame(bf, bean.getBeanFactory());
}
 
Example 19
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonStaticFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition(TestBeanFactory.class);
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertFalse(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);
}
 
Example 20
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testNonStaticPrototypeFactoryMethodFoundByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition factoryBd = new RootBeanDefinition(TestBeanFactory.class);
	lbf.registerBeanDefinition("factory", factoryBd);
	RootBeanDefinition rbd = new RootBeanDefinition();
	rbd.setFactoryBeanName("factory");
	rbd.setFactoryMethodName("createTestBeanNonStatic");
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("x1", rbd);

	TestBeanFactory.initialized = false;
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(1, beanNames.length);
	assertEquals("x1", beanNames[0]);
	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertFalse(lbf.containsBean("&x1"));
	assertTrue(lbf.containsLocalBean("x1"));
	assertFalse(lbf.containsLocalBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("x1", Object.class));
	assertFalse(lbf.isTypeMatch("&x1", Object.class));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(null, lbf.getType("&x1"));
	assertFalse(TestBeanFactory.initialized);

	lbf.registerAlias("x1", "x2");
	assertTrue(lbf.containsBean("x2"));
	assertFalse(lbf.containsBean("&x2"));
	assertTrue(lbf.containsLocalBean("x2"));
	assertFalse(lbf.containsLocalBean("&x2"));
	assertFalse(lbf.isSingleton("x2"));
	assertFalse(lbf.isSingleton("&x2"));
	assertTrue(lbf.isPrototype("x2"));
	assertFalse(lbf.isPrototype("&x2"));
	assertTrue(lbf.isTypeMatch("x2", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
	assertTrue(lbf.isTypeMatch("x2", Object.class));
	assertFalse(lbf.isTypeMatch("&x2", Object.class));
	assertEquals(TestBean.class, lbf.getType("x2"));
	assertEquals(null, lbf.getType("&x2"));
	assertEquals(1, lbf.getAliases("x1").length);
	assertEquals("x2", lbf.getAliases("x1")[0]);
	assertEquals(1, lbf.getAliases("&x1").length);
	assertEquals("&x2", lbf.getAliases("&x1")[0]);
	assertEquals(1, lbf.getAliases("x2").length);
	assertEquals("x1", lbf.getAliases("x2")[0]);
	assertEquals(1, lbf.getAliases("&x2").length);
	assertEquals("&x1", lbf.getAliases("&x2")[0]);
}