Package org.springframework.boot.builder
Class SpringApplicationBuilder
- java.lang.Object
- org.springframework.boot.builder.SpringApplicationBuilder
public class SpringApplicationBuilder extends Object
Builder forSpringApplication
andApplicationContext
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.
- See Also:
SpringApplication
Constructor Summary
Constructors Constructor Description SpringApplicationBuilder(Class<?>... sources)
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 theBanner
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 configuredSpringApplication
that is ready to run.SpringApplication
build(String... args)
Returns a fully configuredSpringApplication
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 newSpringApplication
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 theApplicationContext
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(String... defaultProperties)
Default properties for the environment in the formkey=value
orkey:value
.SpringApplicationBuilder
properties(Map<String,Object> defaults)
Default properties for the environment.SpringApplicationBuilder
properties(Properties defaultProperties)
Default properties for the environment in the formkey=value
orkey:value
.SpringApplicationBuilder
registerShutdownHook(boolean registerShutdownHook)
Sets if the createdApplicationContext
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
setAddConversionService(boolean addConversionService)
Flag to indicate if theApplicationConversionService
should be added to the application context'sEnvironment
.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.
Constructor Detail
SpringApplicationBuilder
public SpringApplicationBuilder(Class<?>... sources)
Method Detail
createSpringApplication
protected SpringApplication createSpringApplication(Class<?>... sources)
Creates a newSpringApplication
instances from the given sources. Subclasses may override in order to provide a custom subclass ofSpringApplication
- Parameters:
sources
- the sources- Returns:
- the
SpringApplication
instance - Since:
- 1.1.0
context
public org.springframework.context.ConfigurableApplicationContext context()
Accessor for the current application context.- Returns:
- the current application context (or null if not yet running)
application
public SpringApplication application()
Accessor for the current application.- Returns:
- the current application (never null)
run
public org.springframework.context.ConfigurableApplicationContext run(String... args)
Create an application context (and its parent if specified) with the command line args provided. The parent is run first with the same arguments if has not yet been started.- Parameters:
args
- the command line arguments- Returns:
- an application context created from the current state
build
public SpringApplication build()
Returns a fully configuredSpringApplication
that is ready to run.- Returns:
- the fully configured
SpringApplication
.
build
public SpringApplication build(String... args)
Returns a fully configuredSpringApplication
that is ready to run. Any parent that has been configured will be run with the givenargs
.- Parameters:
args
- the parent's args- Returns:
- the fully configured
SpringApplication
.
child
public SpringApplicationBuilder child(Class<?>... sources)
Create a child application with the provided sources. Default args and environment are copied down into the child, but everything else is a clean sheet.- Parameters:
sources
- the sources for the application (Spring configuration)- Returns:
- the child application builder
parent
public SpringApplicationBuilder parent(Class<?>... sources)
Add a parent application with the provided sources. Default args and environment are copied up into the parent, but everything else is a clean sheet.- Parameters:
sources
- the sources for the application (Spring configuration)- Returns:
- the parent builder
parent
public SpringApplicationBuilder parent(org.springframework.context.ConfigurableApplicationContext parent)
Add an already running parent context to an existing application.- Parameters:
parent
- the parent context- Returns:
- the current builder (not the parent)
sibling
public SpringApplicationBuilder sibling(Class<?>... sources)
Create a sibling application (one with the same parent). A side effect of calling this method is that the current application (and its parent) are started without any arguments if they are not already running. To supply arguments when starting the current application and its parent usesibling(Class[], String...)
instead.- Parameters:
sources
- the sources for the application (Spring configuration)- Returns:
- the new sibling builder
sibling
public SpringApplicationBuilder sibling(Class<?>[] sources, String... args)
Create a sibling application (one with the same parent). A side effect of calling this method is that the current application (and its parent) are started if they are not already running.- Parameters:
sources
- the sources for the application (Spring configuration)args
- the command line arguments to use when starting the current app and its parent- Returns:
- the new sibling builder
contextClass
public SpringApplicationBuilder contextClass(Class<? extends org.springframework.context.ConfigurableApplicationContext> cls)
Explicitly set the context class to be used.- Parameters:
cls
- the context class to use- Returns:
- the current builder
sources
public SpringApplicationBuilder sources(Class<?>... sources)
Add more sources (configuration classes and components) to this application.- Parameters:
sources
- the sources to add- Returns:
- the current builder
web
public SpringApplicationBuilder web(WebApplicationType webApplicationType)
Flag to explicitly request a specific type of web application. Auto-detected based on the classpath if not set.- Parameters:
webApplicationType
- the type of web application- Returns:
- the current builder
- Since:
- 2.0.0
logStartupInfo
public SpringApplicationBuilder logStartupInfo(boolean logStartupInfo)
Flag to indicate the startup information should be logged.- Parameters:
logStartupInfo
- the flag to set. Default true.- Returns:
- the current builder
banner
public SpringApplicationBuilder banner(Banner banner)
Sets theBanner
instance which will be used to print the banner when no static banner file is provided.- Parameters:
banner
- the banner to use- Returns:
- the current builder
bannerMode
public SpringApplicationBuilder bannerMode(Banner.Mode bannerMode)
headless
public SpringApplicationBuilder headless(boolean headless)
Sets if the application is headless and should not instantiate AWT. Defaults totrue
to prevent java icons appearing.- Parameters:
headless
- if the application is headless- Returns:
- the current builder
registerShutdownHook
public SpringApplicationBuilder registerShutdownHook(boolean registerShutdownHook)
Sets if the createdApplicationContext
should have a shutdown hook registered.- Parameters:
registerShutdownHook
- if the shutdown hook should be registered- Returns:
- the current builder
main
public SpringApplicationBuilder main(Class<?> mainApplicationClass)
Fixes the main application class that is used to anchor the startup messages.- Parameters:
mainApplicationClass
- the class to use.- Returns:
- the current builder
addCommandLineProperties
public SpringApplicationBuilder addCommandLineProperties(boolean addCommandLineProperties)
Flag to indicate that command line arguments should be added to the environment.- Parameters:
addCommandLineProperties
- the flag to set. Default true.- Returns:
- the current builder
setAddConversionService
public SpringApplicationBuilder setAddConversionService(boolean addConversionService)
Flag to indicate if theApplicationConversionService
should be added to the application context'sEnvironment
.- Parameters:
addConversionService
- if the conversion service should be added.- Returns:
- the current builder
- Since:
- 2.1.0
properties
public SpringApplicationBuilder properties(String... defaultProperties)
Default properties for the environment in the formkey=value
orkey:value
.- Parameters:
defaultProperties
- the properties to set.- Returns:
- the current builder
properties
public SpringApplicationBuilder properties(Properties defaultProperties)
Default properties for the environment in the formkey=value
orkey:value
.- Parameters:
defaultProperties
- the properties to set.- Returns:
- the current builder
properties
public SpringApplicationBuilder properties(Map<String,Object> defaults)
Default properties for the environment. Multiple calls to this method are cumulative.- Parameters:
defaults
- the default properties- Returns:
- the current builder
- See Also:
properties(String...)
profiles
public SpringApplicationBuilder profiles(String... profiles)
Add to the active Spring profiles for this app (and its parent and children).- Parameters:
profiles
- the profiles to add.- Returns:
- the current builder
beanNameGenerator
public SpringApplicationBuilder beanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)
Bean name generator for automatically generated bean names in the application context.- Parameters:
beanNameGenerator
- the generator to set.- Returns:
- the current builder
environment
public SpringApplicationBuilder environment(org.springframework.core.env.ConfigurableEnvironment environment)
Environment for the application context.- Parameters:
environment
- the environment to set.- Returns:
- the current builder
resourceLoader
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.- Parameters:
resourceLoader
- the resource loader to set.- Returns:
- the current builder
initializers
public SpringApplicationBuilder initializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
Add some initializers to the application (applied to theApplicationContext
before any bean definitions are loaded).- Parameters:
initializers
- some initializers to add- Returns:
- the current builder
listeners
public 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). Any listeners that are alsoApplicationContextInitializer
will be added to theinitializers
automatically.- Parameters:
listeners
- some listeners to add- Returns:
- the current builder