Package org.kiwiproject.spring.context
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 setup, call
build()to create the ApplicationContext.
-
-
Constructor Summary
Constructors Constructor Description SpringContextBuilder()Create a context builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SpringContextBuilderaddAnnotationConfiguration(Class<?> aClass)Adds an annotation-based Spring@Configurationclass.SpringContextBuilderaddParentContextBean(String name, Object bean)Adds the specified bean to the parent context.SpringContextBuilderaddXmlConfigLocation(String location)Add a single Spring XML configuration location.org.springframework.context.ApplicationContextbuild()Generate the ApplicationContext.SpringContextBuilderwithAnnotationConfigurations(Class<?>... classes)Adds multiple annotation-based Spring@Configurationclasses.SpringContextBuilderwithXmlConfigLocations(String... locations)Add multiple Spring XML configuration locations
-
-
-
Method Detail
-
addParentContextBean
public SpringContextBuilder addParentContextBean(String name, Object bean)
Adds the specified bean to the parent context.- Parameters:
name- the bean namebean- the bean instance- Returns:
- the builder instance
-
addAnnotationConfiguration
public SpringContextBuilder addAnnotationConfiguration(Class<?> aClass)
Adds an annotation-based Spring@Configurationclass.- 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@Configurationclasses.- 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
-
-