Package org.kiwiproject.spring.context
Class SpringContextBuilder
java.lang.Object
org.kiwiproject.spring.context.SpringContextBuilder
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddAnnotationConfiguration(Class<?> aClass) Adds an annotation-based Spring@Configurationclass.addParentContextBean(String name, Object bean) Adds the specified bean to the parent context.addXmlConfigLocation(String location) Add a single Spring XML configuration location.org.springframework.context.ApplicationContextbuild()Generate the ApplicationContext.withAnnotationConfigurations(Class<?>... classes) Adds multiple annotation-based Spring@Configurationclasses.withXmlConfigLocations(String... locations) Add multiple Spring XML configuration locations
-
Constructor Details
-
SpringContextBuilder
public SpringContextBuilder()Create a context builder.
-
-
Method Details
-
addParentContextBean
Adds the specified bean to the parent context.- Parameters:
name- the bean namebean- the bean instance- Returns:
- the builder instance
-
addAnnotationConfiguration
Adds an annotation-based Spring@Configurationclass.- Parameters:
aClass- class containing Spring configuration. Should be a class annotated with@Configuration.- Returns:
- the builder instance
-
withAnnotationConfigurations
Adds multiple annotation-based Spring@Configurationclasses.- Parameters:
classes- classes containing Spring configuration. Should be classes annotated with@Configuration.- Returns:
- the builder instance
-
addXmlConfigLocation
Add a single Spring XML configuration location.- Parameters:
location- the XML config location, e.g.applicationContext.xml- Returns:
- the builder instance
-
withXmlConfigLocations
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
-