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:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user