public class SpringApplicationBuilder extends Object
SpringApplication
and ApplicationContext
instances with
convenient fluent API and context hierarchy support. Simple example of a context
hierarchy:
new SpringApplicationBuilder(ParentConfig.class).child(ChildConfig.class).run(args);Another common use case is setting default arguments, e.g. active Spring profiles, to set up the environment for an application:
new SpringApplicationBuilder(Application.class).profiles("server") .defaultArgs("--transport=local").run(args);
If your needs are simpler, consider using the static convenience methods in SpringApplication instead.
Constructor and Description |
---|
SpringApplicationBuilder(Object... sources) |
Modifier and Type | Method and Description |
---|---|
SpringApplicationBuilder |
addCommandLineProperties(boolean addCommandLineProperties)
Flag to indicate that command line arguments should be added to the environment.
|
SpringApplication |
application()
Accessor for the current application.
|
SpringApplicationBuilder |
banner(Banner banner)
Sets the
Banner instance which will be used to print the banner when no
static banner file is provided. |
SpringApplicationBuilder |
beanNameGenerator(BeanNameGenerator beanNameGenerator)
Bean name generator for automatically generated bean names in the application
context.
|
SpringApplication |
build()
Returns a fully configured
SpringApplication that is ready to run. |
SpringApplicationBuilder |
child(Object... sources)
Create a child application with the provided sources.
|
ConfigurableApplicationContext |
context()
Accessor for the current application context.
|
SpringApplicationBuilder |
contextClass(Class<? extends ConfigurableApplicationContext> cls)
Explicitly set the context class to be used.
|
protected SpringApplication |
createSpringApplication(Object... sources)
Creates a new
SpringApplication instances from the
given sources. |
SpringApplicationBuilder |
environment(ConfigurableEnvironment environment)
Environment for the application context.
|
SpringApplicationBuilder |
headless(boolean headless)
Sets if the application is headless and should not instantiate AWT.
|
SpringApplicationBuilder |
initializers(ApplicationContextInitializer<?>... initializers)
Add some initializers to the application (applied to the
ApplicationContext
before any bean definitions are loaded). |
SpringApplicationBuilder |
listeners(ApplicationListener<?>... listeners)
Add some listeners to the application (listening for SpringApplication events as
well as regular Spring events once the context is running).
|
SpringApplicationBuilder |
logStartupInfo(boolean logStartupInfo)
Flag to indicate the startup information should be logged.
|
SpringApplicationBuilder |
main(Class<?> mainApplicationClass)
Fixes the main application class that is used to anchor the startup messages.
|
SpringApplicationBuilder |
parent(ConfigurableApplicationContext parent)
Add an already running parent context to an existing application.
|
SpringApplicationBuilder |
parent(Object... sources)
Add a parent application with the provided sources.
|
SpringApplicationBuilder |
profiles(String... profiles)
Add to the active Spring profiles for this app (and its parent and children).
|
SpringApplicationBuilder |
properties(Map<String,Object> defaults)
Default properties for the environment.
|
SpringApplicationBuilder |
properties(Properties defaultProperties)
Default properties for the environment in the form
key=value or
key:value . |
SpringApplicationBuilder |
properties(String... defaultProperties)
Default properties for the environment in the form
key=value or
key:value . |
SpringApplicationBuilder |
registerShutdownHook(boolean registerShutdownHook)
Sets if the created
ApplicationContext should have a shutdown hook
registered. |
SpringApplicationBuilder |
resourceLoader(ResourceLoader resourceLoader)
ResourceLoader for the application context. |
ConfigurableApplicationContext |
run(String... args)
Create an application context (and its parent if specified) with the command line
args provided.
|
SpringApplicationBuilder |
showBanner(boolean showBanner)
Flag to indicate the startup banner should be printed.
|
SpringApplicationBuilder |
sibling(Object... sources)
Create a sibling application (one with the same parent).
|
SpringApplicationBuilder |
sibling(Object[] sources,
String... args)
Create a sibling application (one with the same parent).
|
SpringApplicationBuilder |
sources(Class<?>... sources)
Add more sources (configuration classes and components) to this application
|
SpringApplicationBuilder |
sources(Object... sources)
Add more sources to use in this application.
|
SpringApplicationBuilder |
web(boolean webEnvironment)
Flag to explicitly request a web or non-web environment (auto detected based on
classpath if not set).
|
public SpringApplicationBuilder(Object... sources)
protected SpringApplication createSpringApplication(Object... sources)
SpringApplication
instances from the
given sources. Subclasses may override in order to provide a custom subclass of
SpringApplication
sources
- The sourcesSpringApplication
instancepublic ConfigurableApplicationContext context()
public SpringApplication application()
public ConfigurableApplicationContext run(String... args)
args
- the command line argumentspublic SpringApplication build()
SpringApplication
that is ready to run.SpringApplication
.public SpringApplicationBuilder child(Object... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder parent(Object... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder parent(ConfigurableApplicationContext parent)
parent
- the parent contextpublic SpringApplicationBuilder sibling(Object... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder sibling(Object[] sources, String... args)
sources
- the sources for the application (Spring configuration)args
- the command line arguments to use when starting the current app and its
parentpublic SpringApplicationBuilder contextClass(Class<? extends ConfigurableApplicationContext> cls)
cls
- the context class to usepublic SpringApplicationBuilder sources(Object... sources)
sources
- the sources to addpublic SpringApplicationBuilder sources(Class<?>... sources)
sources
- the sources to addpublic SpringApplicationBuilder web(boolean webEnvironment)
webEnvironment
- the flag to setpublic SpringApplicationBuilder logStartupInfo(boolean logStartupInfo)
logStartupInfo
- the flag to set. Default true.public SpringApplicationBuilder banner(Banner banner)
Banner
instance which will be used to print the banner when no
static banner file is provided.banner
- The banner to usepublic SpringApplicationBuilder showBanner(boolean showBanner)
showBanner
- the flag to set. Default true.public SpringApplicationBuilder headless(boolean headless)
true
to prevent java icons appearing.headless
- if the application is headlesspublic SpringApplicationBuilder registerShutdownHook(boolean registerShutdownHook)
ApplicationContext
should have a shutdown hook
registered.registerShutdownHook
- if the shutdown hook should be registeredpublic SpringApplicationBuilder main(Class<?> mainApplicationClass)
mainApplicationClass
- the class to use.public SpringApplicationBuilder addCommandLineProperties(boolean addCommandLineProperties)
addCommandLineProperties
- the flag to set. Default true.public SpringApplicationBuilder properties(String... defaultProperties)
key=value
or
key:value
.defaultProperties
- the properties to set.public SpringApplicationBuilder properties(Properties defaultProperties)
key=value
or
key:value
.defaultProperties
- the properties to set.public SpringApplicationBuilder properties(Map<String,Object> defaults)
defaults
- the default propertiesproperties(String...)
public SpringApplicationBuilder profiles(String... profiles)
profiles
- the profiles to add.public SpringApplicationBuilder beanNameGenerator(BeanNameGenerator beanNameGenerator)
beanNameGenerator
- the generator to set.public SpringApplicationBuilder environment(ConfigurableEnvironment environment)
environment
- the environment to set.public SpringApplicationBuilder resourceLoader(ResourceLoader resourceLoader)
ResourceLoader
for the application context. If a custom class loader is
needed, this is where it would be added.resourceLoader
- the resource loader to set.public SpringApplicationBuilder initializers(ApplicationContextInitializer<?>... initializers)
ApplicationContext
before any bean definitions are loaded).initializers
- some initializers to addpublic SpringApplicationBuilder listeners(ApplicationListener<?>... listeners)
ApplicationContextInitializer
will be added to the
initializers
automatically.listeners
- some listeners to addCopyright © 2015 Pivotal Software, Inc.. All rights reserved.