Allow class-relative resource loading in GenericXmlApplicationContext (SPR-7530)
Before:
- new GenericXmlApplicationContext("com/acme/path/to/resource.xml");
- GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("com/acme/path/to/resource.xml");
ctx.refresh();
After:
- The above remain supported, as well as new class-relative variants
- import com.acme.path.to.Foo;
new GenericXmlApplicationContext(Foo.class, "resource.xml");
- import com.acme.path.to.Foo;
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load(Foo.class, "resource.xml");
ctx.refresh();
These changes are generally aligned with signatures long available in
ClassPathXmlApplicationContext. As GenericXmlApplicationContext is
intended to be a more flexible successor to CPXAC (and FSXAC), it's
important that all the same conveniences are available.
This commit is contained in:
parent
912d349366
commit
6f69b7b752
|
|
@ -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 <code>true</code>.
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<bean id="testBean" class="java.lang.Object"/>
|
||||
|
||||
</beans>
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue