public class SpringApplicationBuilder
extends java.lang.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(java.lang.Class<?>... sources) |
SpringApplicationBuilder(org.springframework.core.io.ResourceLoader resourceLoader,
java.lang.Class<?>... sources) |
Modifier and Type | Method and Description |
---|---|
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.
|
SpringApplicationBuilder |
allowCircularReferences(boolean allowCircularReferences)
Whether to allow circular references between beans and automatically try to resolve
them.
|
SpringApplication |
application()
Accessor for the current application.
|
SpringApplicationBuilder |
applicationStartup(org.springframework.core.metrics.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(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(java.lang.String... args)
Returns a fully configured
SpringApplication that is ready to run. |
SpringApplicationBuilder |
child(java.lang.Class<?>... sources)
Create a child application with the provided sources.
|
org.springframework.context.ConfigurableApplicationContext |
context()
Accessor for the current application context.
|
SpringApplicationBuilder |
contextFactory(ApplicationContextFactory factory)
Explicitly set the factory used to create the application context.
|
protected SpringApplication |
createSpringApplication(java.lang.Class<?>... sources)
Deprecated.
since 2.6.0 for removal in 2.8.0 in favor of
createSpringApplication(ResourceLoader, Class...) |
protected SpringApplication |
createSpringApplication(org.springframework.core.io.ResourceLoader resourceLoader,
java.lang.Class<?>... sources)
Creates a new
SpringApplication instance from the given sources using the
given ResourceLoader . |
SpringApplicationBuilder |
environment(org.springframework.core.env.ConfigurableEnvironment environment)
Environment for the application context.
|
SpringApplicationBuilder |
environmentPrefix(java.lang.String environmentPrefix)
Prefix that should be applied when obtaining configuration properties from the
system environment.
|
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 |
lazyInitialization(boolean lazyInitialization)
Flag to control whether the application should be initialized lazily.
|
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(java.lang.Class<?> mainApplicationClass)
Fixes the main application class that is used to anchor the startup messages.
|
SpringApplicationBuilder |
parent(java.lang.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(java.lang.String... profiles)
Add to the active Spring profiles for this app (and its parent and children).
|
SpringApplicationBuilder |
properties(java.util.Map<java.lang.String,java.lang.Object> defaults)
Default properties for the environment.
|
SpringApplicationBuilder |
properties(java.util.Properties defaultProperties)
Default properties for the environment.Multiple calls to this method are cumulative
and will not clear any previously set properties.
|
SpringApplicationBuilder |
properties(java.lang.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(java.lang.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(java.lang.Class<?>... sources)
Create a sibling application (one with the same parent).
|
SpringApplicationBuilder |
sibling(java.lang.Class<?>[] sources,
java.lang.String... args)
Create a sibling application (one with the same parent).
|
SpringApplicationBuilder |
sources(java.lang.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(java.lang.Class<?>... sources)
public SpringApplicationBuilder(org.springframework.core.io.ResourceLoader resourceLoader, java.lang.Class<?>... sources)
@Deprecated protected SpringApplication createSpringApplication(java.lang.Class<?>... sources)
createSpringApplication(ResourceLoader, Class...)
SpringApplication
instance from the given sources. Subclasses
may override in order to provide a custom subclass of SpringApplication
.sources
- the sourcesSpringApplication
instanceprotected SpringApplication createSpringApplication(org.springframework.core.io.ResourceLoader resourceLoader, java.lang.Class<?>... sources)
SpringApplication
instance from the given sources using the
given ResourceLoader
. Subclasses may override in order to provide a custom
subclass of SpringApplication
.resourceLoader
- the resource loader (can be null)sources
- the sourcesSpringApplication
instancepublic org.springframework.context.ConfigurableApplicationContext context()
public SpringApplication application()
public org.springframework.context.ConfigurableApplicationContext run(java.lang.String... args)
args
- the command line argumentspublic SpringApplication build()
SpringApplication
that is ready to run.SpringApplication
.public SpringApplication build(java.lang.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(java.lang.Class<?>... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder parent(java.lang.Class<?>... sources)
sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder parent(org.springframework.context.ConfigurableApplicationContext parent)
parent
- the parent contextpublic SpringApplicationBuilder sibling(java.lang.Class<?>... sources)
sibling(Class[], String...)
instead.sources
- the sources for the application (Spring configuration)public SpringApplicationBuilder sibling(java.lang.Class<?>[] sources, java.lang.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 contextFactory(ApplicationContextFactory factory)
factory
- the factory to usepublic SpringApplicationBuilder sources(java.lang.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(java.lang.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.public 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(java.lang.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(java.util.Properties defaultProperties)
defaultProperties
- the properties to set.properties(String...)
,
properties(Map)
public SpringApplicationBuilder properties(java.util.Map<java.lang.String,java.lang.Object> defaults)
defaults
- the default propertiesproperties(String...)
,
properties(Properties)
public SpringApplicationBuilder profiles(java.lang.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 environmentPrefix(java.lang.String environmentPrefix)
environmentPrefix
- the environment property prefix to setpublic 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 addpublic SpringApplicationBuilder applicationStartup(org.springframework.core.metrics.ApplicationStartup applicationStartup)
ApplicationStartup
to be used with the
ApplicationContext
for collecting startup metrics.applicationStartup
- the application startup to usepublic SpringApplicationBuilder allowCircularReferences(boolean allowCircularReferences)
allowCircularReferences
- whether circular references are allowedAbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)