diff --git a/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java b/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java index 1ac98d10a89..9346dd2b1ea 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java +++ b/org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java @@ -17,6 +17,7 @@ package org.springframework.context.support; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** @@ -30,6 +31,7 @@ import org.springframework.core.io.Resource; * deliberately override certain bean definitions via an extra configuration file. * * @author Juergen Hoeller + * @author Chris Beams * @since 3.0 * @see #load * @see XmlBeanDefinitionReader @@ -67,6 +69,17 @@ public class GenericXmlApplicationContext extends GenericApplicationContext { refresh(); } + /** + * Create a new GenericXmlApplicationContext, loading bean definitions + * from the given resource locations and automatically refreshing the context. + * @param relativeClass class whose package will be used as a prefix when + * loading each specified resource name + * @param resourceNames relatively-qualified names of resources to load + */ + public GenericXmlApplicationContext(Class relativeClass, String... resourceNames) { + load(relativeClass, resourceNames); + refresh(); + } /** * Set whether to use XML validation. Default is true. @@ -92,4 +105,18 @@ public class GenericXmlApplicationContext extends GenericApplicationContext { this.reader.loadBeanDefinitions(resourceLocations); } + /** + * Load bean definitions from the given XML resources. + * @param relativeClass class whose package will be used as a prefix when + * loading each specified resource name + * @param resourceNames relatively-qualified names of resources to load + */ + public void load(Class relativeClass, String... resourceNames) { + Resource[] resources = new Resource[resourceNames.length]; + for (int i = 0; i < resourceNames.length; i++) { + resources[i] = new ClassPathResource(resourceNames[i], relativeClass); + } + this.load(resources); + } + } diff --git a/org.springframework.context/src/test/java/org/springframework/context/support/GenericXmlApplicationContextTests-context.xml b/org.springframework.context/src/test/java/org/springframework/context/support/GenericXmlApplicationContextTests-context.xml new file mode 100644 index 00000000000..851e41b1bb4 --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/context/support/GenericXmlApplicationContextTests-context.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/org.springframework.context/src/test/java/org/springframework/context/support/GenericXmlApplicationContextTests.java b/org.springframework.context/src/test/java/org/springframework/context/support/GenericXmlApplicationContextTests.java new file mode 100644 index 00000000000..1ead59fe472 --- /dev/null +++ b/org.springframework.context/src/test/java/org/springframework/context/support/GenericXmlApplicationContextTests.java @@ -0,0 +1,53 @@ +package org.springframework.context.support; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.util.ClassUtils; + +/** + * Unit tests for {@link GenericXmlApplicationContext}. + * + * See SPR-7530. + * + * @author Chris Beams + */ +public class GenericXmlApplicationContextTests { + + private static final Class RELATIVE_CLASS = GenericXmlApplicationContextTests.class; + private static final String RESOURCE_BASE_PATH = ClassUtils.classPackageAsResourcePath(RELATIVE_CLASS); + private static final String RESOURCE_NAME = GenericXmlApplicationContextTests.class.getSimpleName() + "-context.xml"; + private static final String FQ_RESOURCE_PATH = RESOURCE_BASE_PATH + '/' + RESOURCE_NAME; + private static final String TEST_BEAN_NAME = "testBean"; + + + @Test + public void classRelativeResourceLoading_ctor() { + ApplicationContext ctx = new GenericXmlApplicationContext(RELATIVE_CLASS, RESOURCE_NAME); + assertThat(ctx.containsBean(TEST_BEAN_NAME), is(true)); + } + + @Test + public void classRelativeResourceLoading_load() { + GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); + ctx.load(RELATIVE_CLASS, RESOURCE_NAME); + ctx.refresh(); + assertThat(ctx.containsBean(TEST_BEAN_NAME), is(true)); + } + + @Test + public void fullyQualifiedResourceLoading_ctor() { + ApplicationContext ctx = new GenericXmlApplicationContext(FQ_RESOURCE_PATH); + assertThat(ctx.containsBean(TEST_BEAN_NAME), is(true)); + } + + @Test + public void fullyQualifiedResourceLoading_load() { + GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); + ctx.load(FQ_RESOURCE_PATH); + ctx.refresh(); + assertThat(ctx.containsBean(TEST_BEAN_NAME), is(true)); + } +}