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 |
addBootstrapper(Bootstrapper bootstrapper)
Deprecated.
since 2.4.5 for removal in 2.6 in favor of
addBootstrapRegistryInitializer(BootstrapRegistryInitializer) |
SpringApplicationBuilder |
addBootstrapRegistryInitializer(BootstrapRegistryInitializer bootstrapRegistryInitializer)
Adds
BootstrapRegistryInitializer instances that can be used to initialize
the BootstrapRegistry . |
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 |
applicationStartup(ApplicationStartup applicationStartup)
Configure the
ApplicationStartup to be used with the
ApplicationContext for collecting startup metrics. |
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(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.
|
ConfigurableApplicationContext |
context()
Accessor for the current application context.
|
SpringApplicationBuilder |
contextClass(Class<? extends ConfigurableApplicationContext> cls)
Deprecated.
since 2.4.0 for removal in 2.6.0 in favor of
contextFactory(ApplicationContextFactory) |
SpringApplicationBuilder |
contextFactory(ApplicationContextFactory factory)
Explicitly set the factory used to create the application context.
|
protected SpringApplication |
createSpringApplication(Class<?>... 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 |
lazyInitialization(boolean lazyInitialization)
Flag to control whether the application should be initialized lazily.
|
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(Class<?>... sources)
Add a parent application with the provided sources.
|
SpringApplicationBuilder |
parent(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.Multiple calls to this method are cumulative
and will not clear any previously set properties.
|
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 |
setAddConversionService(boolean addConversionService)
Flag to indicate if the
ApplicationConversionService should be added to the
application context's Environment . |
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(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 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 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(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
parent@Deprecated public SpringApplicationBuilder contextClass(Class<? extends ConfigurableApplicationContext> cls)
contextFactory(ApplicationContextFactory)
cls
- the context class to usepublic SpringApplicationBuilder contextFactory(ApplicationContextFactory factory)
factory
- the factory to usepublic SpringApplicationBuilder sources(Class<?>... sources)
sources
- the sources to addpublic 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 setAddConversionService(boolean addConversionService)
ApplicationConversionService
should be added to the
application context's Environment
.addConversionService
- if the conversion service should be added.@Deprecated public SpringApplicationBuilder addBootstrapper(Bootstrapper bootstrapper)
addBootstrapRegistryInitializer(BootstrapRegistryInitializer)
Bootstrapper
that can be used to initialize
the BootstrapRegistry
.bootstrapper
- the bootstraperpublic SpringApplicationBuilder addBootstrapRegistryInitializer(BootstrapRegistryInitializer bootstrapRegistryInitializer)
BootstrapRegistryInitializer
instances that can be used to initialize
the BootstrapRegistry
.bootstrapRegistryInitializer
- the bootstrap registry initializer to addpublic SpringApplicationBuilder lazyInitialization(boolean lazyInitialization)
lazyInitialization
- the flag to set. Defaults to false.public SpringApplicationBuilder properties(String... defaultProperties)
key=value
or
key:value
. Multiple calls to this method are cumulative and will not clear
any previously set properties.defaultProperties
- the properties to set.properties(Properties)
,
properties(Map)
public SpringApplicationBuilder properties(Properties defaultProperties)
defaultProperties
- the properties to set.properties(String...)
,
properties(Map)
public SpringApplicationBuilder properties(Map<String,Object> defaults)
defaults
- the default propertiesproperties(String...)
,
properties(Properties)
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 addpublic SpringApplicationBuilder applicationStartup(ApplicationStartup applicationStartup)
ApplicationStartup
to be used with the
ApplicationContext
for collecting startup metrics.applicationStartup
- the application startup to use