| 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.annotation.AnnotationConfigApplicationContext; |
| 8 | |
import org.springframework.context.support.GenericApplicationContext; |
| 9 | |
import org.springframework.core.io.DefaultResourceLoader; |
| 10 | |
import org.springframework.core.io.ResourceLoader; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
public class SpringApplicationContextFactory { |
| 24 | |
|
| 25 | |
private final ApplicationContext parent; |
| 26 | |
private final ClassLoader classLoader; |
| 27 | |
private final String[] resources; |
| 28 | |
|
| 29 | |
public SpringApplicationContextFactory(String... resources) { |
| 30 | 5 | this(SpringApplicationContextFactory.class.getClassLoader(), resources); |
| 31 | 5 | } |
| 32 | |
|
| 33 | |
public SpringApplicationContextFactory(ClassLoader classLoader, String... resources) { |
| 34 | 11 | this(null, classLoader, resources); |
| 35 | 11 | } |
| 36 | |
|
| 37 | 11 | public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resources) { |
| 38 | 11 | this.parent = parent; |
| 39 | 11 | this.classLoader = classLoader; |
| 40 | 11 | this.resources = resources; |
| 41 | 11 | } |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public ConfigurableApplicationContext createApplicationContext() { |
| 52 | |
try { |
| 53 | |
|
| 54 | 11 | Class<?>[] annotatedClasses = new Class<?>[resources.length]; |
| 55 | 13 | for (int i = 0; i < resources.length; i++) { |
| 56 | 11 | annotatedClasses[i] = this.classLoader.loadClass(resources[i]); |
| 57 | |
} |
| 58 | 2 | AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses); |
| 59 | 2 | context.setParent(parent); |
| 60 | 2 | context.setClassLoader(classLoader); |
| 61 | 2 | return context; |
| 62 | 9 | } catch (ClassNotFoundException e) { |
| 63 | |
|
| 64 | 9 | GenericApplicationContext context = new GenericApplicationContext(parent); |
| 65 | 9 | context.setClassLoader(classLoader); |
| 66 | 9 | ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader); |
| 67 | 9 | context.setResourceLoader(resourceLoader); |
| 68 | 9 | BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); |
| 69 | 20 | for (String resource : resources) { |
| 70 | 13 | reader.loadBeanDefinitions(resourceLoader.getResource(resource)); |
| 71 | |
} |
| 72 | 7 | context.refresh(); |
| 73 | 7 | return context; |
| 74 | |
} |
| 75 | |
|
| 76 | |
} |
| 77 | |
|
| 78 | |
} |