| 1 | |
package org.jbehave.core.steps.spring; |
| 2 | |
|
| 3 | |
import org.springframework.beans.factory.support.BeanDefinitionReader; |
| 4 | |
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
| 5 | |
import org.springframework.context.ApplicationContext; |
| 6 | |
import org.springframework.context.ConfigurableApplicationContext; |
| 7 | |
import org.springframework.context.support.GenericApplicationContext; |
| 8 | |
import org.springframework.core.io.DefaultResourceLoader; |
| 9 | |
import org.springframework.core.io.ResourceLoader; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
public class SpringApplicationContextFactory { |
| 15 | |
|
| 16 | |
private final ApplicationContext parent; |
| 17 | |
private final ClassLoader classLoader; |
| 18 | |
private final String[] resources; |
| 19 | |
|
| 20 | |
public SpringApplicationContextFactory(String... resources) { |
| 21 | 3 | this(SpringApplicationContextFactory.class.getClassLoader(), resources); |
| 22 | 3 | } |
| 23 | |
|
| 24 | |
public SpringApplicationContextFactory(ClassLoader classLoader, String... resources) { |
| 25 | 9 | this(null, classLoader, resources); |
| 26 | 9 | } |
| 27 | |
|
| 28 | 9 | public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resources) { |
| 29 | 9 | this.parent = parent; |
| 30 | 9 | this.classLoader = classLoader; |
| 31 | 9 | this.resources = resources; |
| 32 | 9 | } |
| 33 | |
|
| 34 | |
public ConfigurableApplicationContext createApplicationContext() { |
| 35 | |
|
| 36 | 9 | GenericApplicationContext context = new GenericApplicationContext(parent); |
| 37 | 9 | context.setClassLoader(classLoader); |
| 38 | 9 | ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader); |
| 39 | 9 | context.setResourceLoader(resourceLoader); |
| 40 | 9 | BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); |
| 41 | 20 | for (String resource : resources) { |
| 42 | 13 | reader.loadBeanDefinitions(resourceLoader.getResource(resource)); |
| 43 | |
} |
| 44 | 7 | context.refresh(); |
| 45 | 7 | return context; |
| 46 | |
} |
| 47 | |
|
| 48 | |
} |