- Revision
- 1604
- Author
- mauro
- Date
- 2010-03-23 15:10:47 -0500 (Tue, 23 Mar 2010)
Log Message
JBEHAVE-253: Added SpringApplicationContextFactory.
Modified Paths
- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/spring/SpringTraderScenario.java
- trunk/core/jbehave-spring/src/behaviour/java/org/jbehave/scenario/steps/spring/SpringStepsFactoryBehaviour.java
Added Paths
Diff
Modified: trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/spring/SpringTraderScenario.java (1603 => 1604)
--- trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/spring/SpringTraderScenario.java 2010-03-19 18:55:53 UTC (rev 1603) +++ trunk/core/examples/trader/src/main/java/org/jbehave/examples/trader/spring/SpringTraderScenario.java 2010-03-23 20:10:47 UTC (rev 1604) @@ -4,9 +4,9 @@ import org.jbehave.scenario.RunnableScenario; import org.jbehave.scenario.steps.CandidateSteps; import org.jbehave.scenario.steps.StepsConfiguration; +import org.jbehave.scenario.steps.spring.SpringApplicationContextFactory; import org.jbehave.scenario.steps.spring.SpringStepsFactory; import org.springframework.beans.factory.ListableBeanFactory; -import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTraderScenario extends TraderScenario { @@ -21,8 +21,7 @@ } private ListableBeanFactory createBeanFactory() { - return (ListableBeanFactory) new ClassPathXmlApplicationContext( - new String[] { "org/jbehave/examples/trader/spring/steps.xml" }); + return new SpringApplicationContextFactory("org/jbehave/examples/trader/spring/steps.xml").getApplicationContext(); } }
Modified: trunk/core/jbehave-spring/src/behaviour/java/org/jbehave/scenario/steps/spring/SpringStepsFactoryBehaviour.java (1603 => 1604)
--- trunk/core/jbehave-spring/src/behaviour/java/org/jbehave/scenario/steps/spring/SpringStepsFactoryBehaviour.java 2010-03-19 18:55:53 UTC (rev 1603) +++ trunk/core/jbehave-spring/src/behaviour/java/org/jbehave/scenario/steps/spring/SpringStepsFactoryBehaviour.java 2010-03-23 20:10:47 UTC (rev 1604) @@ -13,7 +13,6 @@ import org.junit.Test; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.ListableBeanFactory; -import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringStepsFactoryBehaviour { @@ -26,7 +25,7 @@ } private ListableBeanFactory createBeanFactory(String... xmlResources) { - return (ListableBeanFactory)new ClassPathXmlApplicationContext(xmlResources); + return new SpringApplicationContextFactory(xmlResources).getApplicationContext(); } @Test
Added: trunk/core/jbehave-spring/src/main/java/org/jbehave/scenario/steps/spring/SpringApplicationContextFactory.java (0 => 1604)
--- trunk/core/jbehave-spring/src/main/java/org/jbehave/scenario/steps/spring/SpringApplicationContextFactory.java (rev 0) +++ trunk/core/jbehave-spring/src/main/java/org/jbehave/scenario/steps/spring/SpringApplicationContextFactory.java 2010-03-23 20:10:47 UTC (rev 1604) @@ -0,0 +1,50 @@ +package org.jbehave.scenario.steps.spring; + +import org.springframework.beans.factory.support.BeanDefinitionReader; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.ResourceLoader; + +/** + * Simple facade to create a {...@genericapplicationcontext} using an + * injected {...@link ClassLoader}. + */ +public class SpringApplicationContextFactory { + + private GenericApplicationContext context; + + public SpringApplicationContextFactory(String... resourceLocations) { + this(Thread.currentThread().getContextClassLoader(), resourceLocations); + } + + public SpringApplicationContextFactory(ClassLoader classLoader, String... resourceLocations) { + this(null, classLoader, resourceLocations); + } + + public SpringApplicationContextFactory(ApplicationContext parent, String... resourceLocations) { + this(parent, parent.getClassLoader(), resourceLocations); + } + + public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resourceLocations) { + // set up the context class loader + Thread.currentThread().setContextClassLoader(classLoader); + + // create application context + context = new GenericApplicationContext(parent); + ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader); + context.setResourceLoader(resourceLoader); + BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); + for (String resourceLocation : resourceLocations) { + reader.loadBeanDefinitions(resourceLoader.getResource(resourceLocation)); + } + context.refresh(); + } + + public ConfigurableApplicationContext getApplicationContext() { + return context; + } + +}
To unsubscribe from this list please visit: