Class SpringContextBuilder

java.lang.Object
org.kiwiproject.spring.context.SpringContextBuilder

public class SpringContextBuilder extends Object
Builder class for easily constructing Spring ApplicationContext instances using either XML or annotation-based configuration. The generated ApplicationContext instance is built by creating a parent context, which contains registered singleton beans, and then a child context. This allows specific singletons, e.g. a data source or a Dropwizard configuration object, to be accessible from the child context. The parent context beans can be referenced in either XML or annotation configurations.

The methods return an instance of this class, so they can be changed together. Once the configuration is set up, call build() to create the ApplicationContext.

  • Constructor Details

    • SpringContextBuilder

      public SpringContextBuilder()
      Create a context builder.
  • Method Details

    • addParentContextBean

      public SpringContextBuilder addParentContextBean(String name, Object bean)
      Adds the specified bean to the parent context.
      Parameters:
      name - the bean name
      bean - the bean instance
      Returns:
      the builder instance
    • addAnnotationConfiguration

      public SpringContextBuilder addAnnotationConfiguration(Class<?> aClass)
      Adds an annotation-based Spring @Configuration class.
      Parameters:
      aClass - class containing Spring configuration. Should be a class annotated with @Configuration.
      Returns:
      the builder instance
    • withAnnotationConfigurations

      public SpringContextBuilder withAnnotationConfigurations(Class<?>... classes)
      Adds multiple annotation-based Spring @Configuration classes.
      Parameters:
      classes - classes containing Spring configuration. Should be classes annotated with @Configuration.
      Returns:
      the builder instance
    • addXmlConfigLocation

      public SpringContextBuilder addXmlConfigLocation(String location)
      Add a single Spring XML configuration location.
      Parameters:
      location - the XML config location, e.g. applicationContext.xml
      Returns:
      the builder instance
    • withXmlConfigLocations

      public SpringContextBuilder withXmlConfigLocations(String... locations)
      Add multiple Spring XML configuration locations
      Parameters:
      locations - the XML config locations, e.g. applicationContext-core.xml, applicationContext-dao.xml
      Returns:
      the builder instance
    • build

      public org.springframework.context.ApplicationContext build()
      Generate the ApplicationContext.
      Returns:
      the ApplicationContext defined by this builder