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 active profiles and default properties to set up the environment for an application:
new SpringApplicationBuilder(Application.class).profiles("server") .properties("transport=local").run(args);
If your needs are simpler, consider using the static convenience methods in SpringApplication instead.
SpringApplication
Constructor and Description |
---|
SpringApplicationBuilder(Class<?>... 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 |
bannerMode(Banner.Mode bannerMode) |
SpringApplicationBuilder |
beanNameGenerator(org.springframework.beans.factory.support.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. |
SpringApplication |
build(String... args)
Returns a fully configured
SpringApplication that is ready to run. |
SpringApplicationBuilder |
child(Class<?>... sources)
Create a child application with the provided sources.
|
org.springframework.context.ConfigurableApplicationContext |
context()
Accessor for the current application context.
|
SpringApplicationBuilder |
contextClass(Class<? extends org.springframework.context.ConfigurableApplicationContext> cls)
Explicitly set the context class to be used.
|
protected SpringApplication |
createSpringApplication(Class<?>... sources)
Creates a new
SpringApplication instances from the
given sources. |
SpringApplicationBuilder |
environment(org.springframework.core.env.ConfigurableEnvironment environment)
Environment for the application context.
|
SpringApplicationBuilder |
headless(boolean headless)
Sets if the application is headless and should not instantiate AWT.
|
SpringApplicationBuilder |
initializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
Add some initializers to the application (applied to the
ApplicationContext
before any bean definitions are loaded). |
SpringApplicationBuilder |
listeners(org.springframework.context.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(Class<?>... sources)
Add a parent application with the provided sources.
|
SpringApplicationBuilder |
parent(org.springframework.context.ConfigurableApplicationContext parent)
Add an already running parent context to an existing application.
|
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(org.springframework.core.io.ResourceLoader resourceLoader)
ResourceLoader for the application context. |
org.springframework.context.ConfigurableApplicationContext |
run(String... args)
Create an application context (and its parent if specified) with the command line
args provided.
|
SpringApplicationBuilder |
sibling(Class<?>... sources)
Create a sibling application (one with the same parent).
|
SpringApplicationBuilder |
sibling(Class<?>[] 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 |
web(boolean webEnvironment)
Deprecated.
since 2.0.0 in favour of
web(WebApplicationType) |
SpringApplicationBuilder |
web(WebApplicationType webApplicationType)
Flag to explicitly request a specific type of web application.
|
public SpringApplicationBuilder(Class<?>... sources)
protected SpringApplication createSpringApplication(Class<?>... sources)
SpringApplication
instances from the
given sources. Subclasses may override in order to provide a custom subclass of
SpringApplication
sources
- The sourcesSpringApplication
instancepublic org.springframework.context.ConfigurableApplicationContext context()
public SpringApplication application()
public org.springframework.context.ConfigurableApplicationContext run(String... args)
args
- the command line argumentspublic SpringApplication build()
SpringApplication
that is ready to run.SpringApplication
.public SpringApplication build(String... args)
SpringApplication
that is ready to run. Any
parent that has been configured will be run with the given args
.args
- the parent's argsSpringApplication
.public SpringApplicationBuilder child(Class<?>... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder parent(Class<?>... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder parent(org.springframework.context.ConfigurableApplicationContext parent)
parent
- the parent contextpublic SpringApplicationBuilder sibling(Class<?>... sources)
sibling(Class[], String...)
instead.sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder sibling(Class<?>[] 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 org.springframework.context.ConfigurableApplicationContext> cls)
cls
- the context class to usepublic SpringApplicationBuilder sources(Class<?>... sources)
sources
- the sources to add@Deprecated public SpringApplicationBuilder web(boolean webEnvironment)
web(WebApplicationType)
webEnvironment
- the flag to setpublic SpringApplicationBuilder web(WebApplicationType webApplicationType)
webApplicationType
- the type of web applicationpublic 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 bannerMode(Banner.Mode bannerMode)
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(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)
beanNameGenerator
- the generator to set.public SpringApplicationBuilder environment(org.springframework.core.env.ConfigurableEnvironment environment)
environment
- the environment to set.public SpringApplicationBuilder resourceLoader(org.springframework.core.io.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(org.springframework.context.ApplicationContextInitializer<?>... initializers)
ApplicationContext
before any bean definitions are loaded).initializers
- some initializers to addpublic SpringApplicationBuilder listeners(org.springframework.context.ApplicationListener<?>... listeners)
ApplicationContextInitializer
will be added to the
initializers
automatically.listeners
- some listeners to addCopyright © 2017 Pivotal Software, Inc.. All rights reserved.