SpringApplicationContextFactory.java

  1. package org.jbehave.core.steps.spring;

  2. import org.springframework.beans.factory.support.BeanDefinitionReader;
  3. import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.ConfigurableApplicationContext;
  6. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  7. import org.springframework.context.support.GenericApplicationContext;
  8. import org.springframework.core.io.DefaultResourceLoader;
  9. import org.springframework.core.io.ResourceLoader;

  10. /**
  11.  * Factory for Spring {@link ApplicationContext} using the specified resources.
  12.  * The resources can be expressed as:
  13.  * <ol>
  14.  * <li>Annotated class names</li>
  15.  * <li>XML location paths</li>
  16.  * </ol>
  17.  * The context will be an instance of {@link AnnotationConfigApplicationContext},
  18.  * if the resources are annotated class names, or
  19.  * {@link GenericApplicationContext} otherwise.
  20.  */
  21. public class SpringApplicationContextFactory {

  22.     private final ApplicationContext parent;
  23.     private final ClassLoader classLoader;
  24.     private final String[] resources;

  25.     public SpringApplicationContextFactory(String... resources) {
  26.         this(SpringApplicationContextFactory.class.getClassLoader(), resources);
  27.     }

  28.     public SpringApplicationContextFactory(ClassLoader classLoader, String... resources) {
  29.         this(null, classLoader, resources);
  30.     }

  31.     public SpringApplicationContextFactory(ApplicationContext parent, ClassLoader classLoader, String... resources) {
  32.         this.parent = parent;
  33.         this.classLoader = classLoader;
  34.         this.resources = resources;
  35.     }

  36.     /**
  37.      * Creates a configurable application context from the resources provided.
  38.      * The context will be an instance of
  39.      * {@link AnnotationConfigApplicationContext}, if the resources are
  40.      * annotated class names, or {@link GenericApplicationContext} otherwise.
  41.      *
  42.      * @return A ConfigurableApplicationContext
  43.      */
  44.     public ConfigurableApplicationContext createApplicationContext() {
  45.         try {
  46.             // first try to create annotation config application context
  47.             Class<?>[] annotatedClasses = new Class<?>[resources.length];
  48.             for (int i = 0; i < resources.length; i++) {
  49.                 annotatedClasses[i] = this.classLoader.loadClass(resources[i]);
  50.             }
  51.             AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses);
  52.             context.setParent(parent);
  53.             context.setClassLoader(classLoader);
  54.             return context;
  55.         } catch (ClassNotFoundException e) {
  56.             // create generic application context
  57.             GenericApplicationContext context = new GenericApplicationContext(parent);
  58.             context.setClassLoader(classLoader);
  59.             ResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);
  60.             context.setResourceLoader(resourceLoader);
  61.             BeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
  62.             for (String resource : resources) {
  63.                 reader.loadBeanDefinitions(resourceLoader.getResource(resource));
  64.             }
  65.             context.refresh();
  66.             return context;
  67.         }

  68.     }

  69. }