Java Code Examples for org.springframework.beans.factory.support.BeanDefinitionReader#loadBeanDefinitions()

The following examples show how to use org.springframework.beans.factory.support.BeanDefinitionReader#loadBeanDefinitions() . 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: QualifierAnnotationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testQualifiedByParentValue() {
	StaticApplicationContext parent = new StaticApplicationContext();
	GenericBeanDefinition parentLarry = new GenericBeanDefinition();
	parentLarry.setBeanClass(Person.class);
	parentLarry.getPropertyValues().add("name", "ParentLarry");
	parentLarry.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "parentLarry"));
	parent.registerBeanDefinition("someLarry", parentLarry);
	GenericBeanDefinition otherLarry = new GenericBeanDefinition();
	otherLarry.setBeanClass(Person.class);
	otherLarry.getPropertyValues().add("name", "OtherLarry");
	otherLarry.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "otherLarry"));
	parent.registerBeanDefinition("someOtherLarry", otherLarry);
	parent.refresh();

	StaticApplicationContext context = new StaticApplicationContext(parent);
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByParentValueTestBean.class);
	context.refresh();
	QualifiedByParentValueTestBean testBean = (QualifiedByParentValueTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("ParentLarry", person.getName());
}
 
Example 2
Source File: QualifierAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testQualifiedByValue() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByValueTestBean.class);
	context.refresh();
	QualifiedByValueTestBean testBean = (QualifiedByValueTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("Larry", person.getName());
}
 
Example 3
Source File: QualifierAnnotationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testQualifiedByParameterName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByParameterNameTestBean.class);
	context.refresh();
	QualifiedByParameterNameTestBean testBean = (QualifiedByParameterNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
}
 
Example 4
Source File: QualifierAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifiedByFieldName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByFieldNameTestBean.class);
	context.refresh();
	QualifiedByFieldNameTestBean testBean = (QualifiedByFieldNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
}
 
Example 5
Source File: QualifierAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifiedByAlias() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByAliasTestBean.class);
	context.refresh();
	QualifiedByAliasTestBean testBean = (QualifiedByAliasTestBean) context.getBean("testBean");
	Person person = testBean.getStooge();
	assertEquals("LarryBean", person.getName());
}
 
Example 6
Source File: CustomAutowireConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCustomResolver() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
	reader.loadBeanDefinitions(CONTEXT);
	CustomAutowireConfigurer cac = new CustomAutowireConfigurer();
	CustomResolver customResolver = new CustomResolver();
	bf.setAutowireCandidateResolver(customResolver);
	cac.postProcessBeanFactory(bf);
	TestBean testBean = (TestBean) bf.getBean("testBean");
	assertEquals("#1!", testBean.getName());
}
 
Example 7
Source File: QualifierAnnotationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testQualifiedByCustomValue() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByCustomValueTestBean.class);
	context.refresh();
	QualifiedByCustomValueTestBean testBean = (QualifiedByCustomValueTestBean) context.getBean("testBean");
	Person person = testBean.getCurly();
	assertEquals("Curly", person.getName());
}
 
Example 8
Source File: QualifierAnnotationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testQualifiedByAttributesFailsWithoutCustomQualifierRegistered() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByAttributesTestBean.class);
	try {
		context.refresh();
		fail("should have thrown a BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getMessage().contains("found 6"));
	}
}
 
Example 9
Source File: QualifierAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifiedByAttributesFailsWithoutCustomQualifierRegistered() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByAttributesTestBean.class);
	try {
		context.refresh();
		fail("should have thrown a BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getMessage().contains("found 6"));
	}
}
 
Example 10
Source File: QualifierAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testQualifiedByAnnotationValue() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByAnnotationValueTestBean.class);
	context.refresh();
	QualifiedByAnnotationValueTestBean testBean = (QualifiedByAnnotationValueTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarrySpecial", person.getName());
}
 
Example 11
Source File: QualifierAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifiedByValue() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByValueTestBean.class);
	context.refresh();
	QualifiedByValueTestBean testBean = (QualifiedByValueTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("Larry", person.getName());
}
 
Example 12
Source File: QualifierAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifiedByAnnotationValue() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByAnnotationValueTestBean.class);
	context.refresh();
	QualifiedByAnnotationValueTestBean testBean = (QualifiedByAnnotationValueTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarrySpecial", person.getName());
}
 
Example 13
Source File: QualifierAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testQualifiedByParameterName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByParameterNameTestBean.class);
	context.refresh();
	QualifiedByParameterNameTestBean testBean = (QualifiedByParameterNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
}
 
Example 14
Source File: QualifierAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testQualifiedByParameterName() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", QualifiedByParameterNameTestBean.class);
	context.refresh();
	QualifiedByParameterNameTestBean testBean = (QualifiedByParameterNameTestBean) context.getBean("testBean");
	Person person = testBean.getLarry();
	assertEquals("LarryBean", person.getName());
}
 
Example 15
Source File: QualifierAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNonQualifiedFieldFails() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
	context.registerSingleton("testBean", NonQualifiedTestBean.class);
	try {
		context.refresh();
		fail("Should have thrown a BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getMessage().contains("found 6"));
	}
}
 
Example 16
Source File: ConfigurationClassBeanDefinitionReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void loadBeanDefinitionsFromImportedResources(
		Map<String, Class<? extends BeanDefinitionReader>> importedResources) {

	Map<Class<?>, BeanDefinitionReader> readerInstanceCache = new HashMap<Class<?>, BeanDefinitionReader>();

	for (Map.Entry<String, Class<? extends BeanDefinitionReader>> entry : importedResources.entrySet()) {
		String resource = entry.getKey();
		Class<? extends BeanDefinitionReader> readerClass = entry.getValue();

		// Default reader selection necessary?
		if (BeanDefinitionReader.class == readerClass) {
			if (StringUtils.endsWithIgnoreCase(resource, ".groovy")) {
				// When clearly asking for Groovy, that's what they'll get...
				readerClass = GroovyBeanDefinitionReader.class;
			}
			else {
				// Primarily ".xml" files but for any other extension as well
				readerClass = XmlBeanDefinitionReader.class;
			}
		}

		BeanDefinitionReader reader = readerInstanceCache.get(readerClass);
		if (reader == null) {
			try {
				// Instantiate the specified BeanDefinitionReader
				reader = readerClass.getConstructor(BeanDefinitionRegistry.class).newInstance(this.registry);
				// Delegate the current ResourceLoader to it if possible
				if (reader instanceof AbstractBeanDefinitionReader) {
					AbstractBeanDefinitionReader abdr = ((AbstractBeanDefinitionReader) reader);
					abdr.setResourceLoader(this.resourceLoader);
					abdr.setEnvironment(this.environment);
				}
				readerInstanceCache.put(readerClass, reader);
			}
			catch (Exception ex) {
				throw new IllegalStateException(
						"Could not instantiate BeanDefinitionReader class [" + readerClass.getName() + "]");
			}
		}

		// TODO SPR-6310: qualify relative path locations as done in AbstractContextLoader.modifyLocations
		reader.loadBeanDefinitions(resource);
	}
}
 
Example 17
Source File: CollectionMergingTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
	reader.loadBeanDefinitions(new ClassPathResource("collectionMerging.xml", getClass()));
}
 
Example 18
Source File: CollectionMergingTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
	reader.loadBeanDefinitions(new ClassPathResource("collectionMerging.xml", getClass()));
}
 
Example 19
Source File: QualifierAnnotationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testInterfaceWithOneQualifiedFactoryAndOneQualifiedBean() {
	StaticApplicationContext context = new StaticApplicationContext();
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(CONFIG_LOCATION);
}
 
Example 20
Source File: CollectionMergingTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
	BeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
	reader.loadBeanDefinitions(new ClassPathResource("collectionMerging.xml", getClass()));
}