public class SpringApplication extends Object
ApplicationContext
instance (depending on your
classpath)CommandLinePropertySource
to expose command line arguments as
Spring propertiesCommandLineRunner
beansrun(Object, String[])
method can be called
directly from your main method to bootstrap your application:
@Configuration @EnableAutoConfiguration public class MyApplication { // ... Bean definitions public static void main(String[] args) throws Exception { SpringApplication.run(MyApplication.class, args); }
For more advanced configuration a SpringApplication
instance can be created and
customized before being run:
public static void main(String[] args) throws Exception { SpringApplication app = new SpringApplication(MyApplication.class); // ... customize app settings here app.run(args) }
SpringApplication
s can read beans from a variety of different sources. It is
generally recommended that a single @Configuration
class is used to bootstrap
your application, however, any of the following sources can also be used:
Class
- A Java class to be loaded by AnnotatedBeanDefinitionReader
Resource
- An XML resource to be loaded by XmlBeanDefinitionReader
,
or a groovy script to be loaded by GroovyBeanDefinitionReader
Package
- A Java package to be scanned by
ClassPathBeanDefinitionScanner
CharSequence
- A class name, resource handle or package name to loaded as
appropriate. If the CharSequence
cannot be resolved to class and does not
resolve to a Resource
that exists it will be considered a Package
.run(Object, String[])
,
run(Object[], String[])
,
SpringApplication(Object...)
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_WEB_CONTEXT_CLASS |
Constructor and Description |
---|
SpringApplication(Object... sources)
Create a new
SpringApplication instance. |
SpringApplication(ResourceLoader resourceLoader,
Object... sources)
Create a new
SpringApplication instance. |
Modifier and Type | Method and Description |
---|---|
void |
addInitializers(ApplicationContextInitializer<?>... initializers)
Add
ApplicationContextInitializer s to be applied to the Spring
ApplicationContext . |
void |
addListeners(ApplicationListener<?>... listeners)
Add
ApplicationListener s to be applied to the SpringApplication and
registered with the ApplicationContext . |
protected void |
afterRefresh(ConfigurableApplicationContext context,
ApplicationArguments args)
Called after the context has been refreshed.
|
protected void |
afterRefresh(ConfigurableApplicationContext context,
String[] args)
Deprecated.
in 1.3 in favor of
afterRefresh(ConfigurableApplicationContext, ApplicationArguments) |
protected void |
applyInitializers(ConfigurableApplicationContext context)
Apply any
ApplicationContextInitializer s to the context before it is
refreshed. |
protected void |
configureEnvironment(ConfigurableEnvironment environment,
String[] args)
Template method delegating to
configurePropertySources(ConfigurableEnvironment, String[]) and
configureProfiles(ConfigurableEnvironment, String[]) in that order. |
protected void |
configureProfiles(ConfigurableEnvironment environment,
String[] args)
Configure which profiles are active (or active by default) for this application
environment.
|
protected void |
configurePropertySources(ConfigurableEnvironment environment,
String[] args)
Add, remove or re-order any
PropertySource s in this application's
environment. |
protected ConfigurableApplicationContext |
createApplicationContext()
Strategy method used to create the
ApplicationContext . |
protected org.springframework.boot.BeanDefinitionLoader |
createBeanDefinitionLoader(BeanDefinitionRegistry registry,
Object[] sources)
Factory method used to create the
BeanDefinitionLoader . |
static int |
exit(ApplicationContext context,
ExitCodeGenerator... exitCodeGenerators)
Static helper that can be used to exit a
SpringApplication and obtain a
code indicating success (0) or otherwise. |
protected org.apache.commons.logging.Log |
getApplicationLog()
Returns the
Log for the application. |
ClassLoader |
getClassLoader()
Either the ClassLoader that will be used in the ApplicationContext (if
resourceLoader is set, or the context
class loader (if not null), or the loader of the Spring ClassUtils class. |
Set<ApplicationContextInitializer<?>> |
getInitializers()
Returns read-only ordered Set of the
ApplicationContextInitializer s that
will be applied to the Spring ApplicationContext . |
Set<ApplicationListener<?>> |
getListeners()
Returns read-only ordered Set of the
ApplicationListener s that will be
applied to the SpringApplication and registered with the ApplicationContext
. |
ResourceLoader |
getResourceLoader()
The ResourceLoader that will be used in the ApplicationContext.
|
Set<Object> |
getSources()
Returns a mutable set of the sources that will be added to an ApplicationContext
when
run(String...) is called. |
protected void |
load(ApplicationContext context,
Object[] sources)
Load beans into the application context.
|
protected void |
logStartupInfo(boolean isRoot)
Called to log startup information, subclasses may override to add additional
logging.
|
static void |
main(String[] args)
A basic main that can be used to launch an application.
|
protected void |
postProcessApplicationContext(ConfigurableApplicationContext context)
Apply any relevant post processing the
ApplicationContext . |
protected void |
printBanner(Environment environment)
Print a custom banner message to the console, optionally extracting its location or
content from the Environment (banner.location and banner.charset).
|
protected void |
refresh(ApplicationContext applicationContext)
Refresh the underlying
ApplicationContext . |
static ConfigurableApplicationContext |
run(Object[] sources,
String[] args)
Static helper that can be used to run a
SpringApplication from the
specified sources using default settings and user supplied arguments. |
static ConfigurableApplicationContext |
run(Object source,
String... args)
Static helper that can be used to run a
SpringApplication from the
specified source using default settings. |
ConfigurableApplicationContext |
run(String... args)
Run the Spring application, creating and refreshing a new
ApplicationContext . |
void |
setAddCommandLineProperties(boolean addCommandLineProperties)
Sets if a
CommandLinePropertySource should be added to the application
context in order to expose arguments. |
void |
setAdditionalProfiles(String... profiles)
Set additional profile values to use (on top of those set in system or command line
properties).
|
void |
setApplicationContextClass(Class<? extends ConfigurableApplicationContext> applicationContextClass)
Sets the type of Spring
ApplicationContext that will be created. |
void |
setBanner(Banner banner)
Sets the
Banner instance which will be used to print the banner when no
static banner file is provided. |
void |
setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Sets the bean name generator that should be used when generating bean names.
|
void |
setDefaultProperties(Map<String,Object> defaultProperties)
Set default environment properties which will be used in addition to those in the
existing
Environment . |
void |
setDefaultProperties(Properties defaultProperties)
Convenient alternative to
setDefaultProperties(Map) . |
void |
setEnvironment(ConfigurableEnvironment environment)
Sets the underlying environment that should be used with the created application
context.
|
void |
setHeadless(boolean headless)
Sets if the application is headless and should not instantiate AWT.
|
void |
setInitializers(Collection<? extends ApplicationContextInitializer<?>> initializers)
Sets the
ApplicationContextInitializer that will be applied to the Spring
ApplicationContext . |
void |
setListeners(Collection<? extends ApplicationListener<?>> listeners)
Sets the
ApplicationListener s that will be applied to the SpringApplication
and registered with the ApplicationContext . |
void |
setLogStartupInfo(boolean logStartupInfo)
Sets if the application information should be logged when the application starts.
|
void |
setMainApplicationClass(Class<?> mainApplicationClass)
Set a specific main application class that will be used as a log source and to
obtain version information.
|
void |
setRegisterShutdownHook(boolean registerShutdownHook)
Sets if the created
ApplicationContext should have a shutdown hook
registered. |
void |
setResourceLoader(ResourceLoader resourceLoader)
Sets the
ResourceLoader that should be used when loading resources. |
void |
setShowBanner(boolean showBanner)
Sets if the Spring banner should be displayed when the application runs.
|
void |
setSources(Set<Object> sources)
The sources that will be used to create an ApplicationContext.
|
void |
setWebEnvironment(boolean webEnvironment)
Sets if this application is running within a web environment.
|
public static final String DEFAULT_WEB_CONTEXT_CLASS
public SpringApplication(Object... sources)
SpringApplication
instance. The application context will load
beans from the specified sources (see class-level
documentation for details. The instance can be customized before calling
run(String...)
.sources
- the bean sourcesrun(Object, String[])
,
SpringApplication(ResourceLoader, Object...)
public SpringApplication(ResourceLoader resourceLoader, Object... sources)
SpringApplication
instance. The application context will load
beans from the specified sources (see class-level
documentation for details. The instance can be customized before calling
run(String...)
.resourceLoader
- the resource loader to usesources
- the bean sourcesrun(Object, String[])
,
SpringApplication(ResourceLoader, Object...)
public ConfigurableApplicationContext run(String... args)
ApplicationContext
.args
- the application arguments (usually passed from a Java main method)ApplicationContext
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args)
configurePropertySources(ConfigurableEnvironment, String[])
and
configureProfiles(ConfigurableEnvironment, String[])
in that order.
Override this method for complete control over Environment customization, or one of
the above for fine-grained control over property sources or profiles, respectively.environment
- this application's environmentargs
- arguments passed to the run
methodconfigureProfiles(ConfigurableEnvironment, String[])
,
configurePropertySources(ConfigurableEnvironment, String[])
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args)
PropertySource
s in this application's
environment.environment
- this application's environmentargs
- arguments passed to the run
methodconfigureEnvironment(ConfigurableEnvironment, String[])
protected void configureProfiles(ConfigurableEnvironment environment, String[] args)
environment
- this application's environmentargs
- arguments passed to the run
methodconfigureEnvironment(ConfigurableEnvironment, String[])
protected void printBanner(Environment environment)
environment
- the environmentsetShowBanner(boolean)
protected ConfigurableApplicationContext createApplicationContext()
ApplicationContext
. By default this
method will respect any explicitly set application context or application context
class before falling back to a suitable default.setApplicationContextClass(Class)
protected void postProcessApplicationContext(ConfigurableApplicationContext context)
ApplicationContext
. Subclasses can
apply additional processing as required.context
- the application contextprotected void applyInitializers(ConfigurableApplicationContext context)
ApplicationContextInitializer
s to the context before it is
refreshed.context
- the configured ApplicationContext (not refreshed yet)ConfigurableApplicationContext.refresh()
protected void logStartupInfo(boolean isRoot)
isRoot
- true if this application is the root of a context hierarchyprotected org.apache.commons.logging.Log getApplicationLog()
Log
for the application. By default will be deduced.protected void load(ApplicationContext context, Object[] sources)
context
- the context to load beans intosources
- the sources to loadpublic ResourceLoader getResourceLoader()
public ClassLoader getClassLoader()
resourceLoader
is set, or the context
class loader (if not null), or the loader of the Spring ClassUtils
class.protected org.springframework.boot.BeanDefinitionLoader createBeanDefinitionLoader(BeanDefinitionRegistry registry, Object[] sources)
BeanDefinitionLoader
.registry
- the bean definition registrysources
- the sources to loadBeanDefinitionLoader
that will be used to load beansprotected void refresh(ApplicationContext applicationContext)
ApplicationContext
.applicationContext
- the application context to refreshprotected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args)
context
- the application contextargs
- the application argumments@Deprecated protected void afterRefresh(ConfigurableApplicationContext context, String[] args)
afterRefresh(ConfigurableApplicationContext, ApplicationArguments)
context
- the application contextargs
- the application argummentspublic void setMainApplicationClass(Class<?> mainApplicationClass)
null
if there is no explicit application class.mainApplicationClass
- the mainApplicationClass to set or null
public void setWebEnvironment(boolean webEnvironment)
webEnvironment
- if the application is running in a web environmentpublic void setHeadless(boolean headless)
true
to prevent java icons appearing.headless
- if the application is headlesspublic void setRegisterShutdownHook(boolean registerShutdownHook)
ApplicationContext
should have a shutdown hook
registered. Defaults to true
to ensure that JVM shutdowns are handled
gracefully.registerShutdownHook
- if the shutdown hook should be registeredpublic void setBanner(Banner banner)
Banner
instance which will be used to print the banner when no
static banner file is provided.banner
- The Banner instance to usepublic void setShowBanner(boolean showBanner)
true
.showBanner
- if the banner should be shownpublic void setLogStartupInfo(boolean logStartupInfo)
true
.logStartupInfo
- if startup info should be logged.public void setAddCommandLineProperties(boolean addCommandLineProperties)
CommandLinePropertySource
should be added to the application
context in order to expose arguments. Defaults to true
.addCommandLineProperties
- if command line arguments should be exposedpublic void setDefaultProperties(Map<String,Object> defaultProperties)
Environment
.defaultProperties
- the additional properties to setpublic void setDefaultProperties(Properties defaultProperties)
setDefaultProperties(Map)
.defaultProperties
- some Properties
public void setAdditionalProfiles(String... profiles)
profiles
- the additional profiles to setpublic void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
beanNameGenerator
- the bean name generatorpublic void setEnvironment(ConfigurableEnvironment environment)
environment
- the environmentpublic Set<Object> getSources()
run(String...)
is called.SpringApplication(Object...)
public void setSources(Set<Object> sources)
run(Object[], String[])
).
NOTE: sources defined here will be used in addition to any sources specified on construction.
sources
- the sources to setSpringApplication(Object...)
public void setResourceLoader(ResourceLoader resourceLoader)
ResourceLoader
that should be used when loading resources.resourceLoader
- the resource loaderpublic void setApplicationContextClass(Class<? extends ConfigurableApplicationContext> applicationContextClass)
ApplicationContext
that will be created. If not
specified defaults to DEFAULT_WEB_CONTEXT_CLASS
for web based applications
or AnnotationConfigApplicationContext
for non web based applications.applicationContextClass
- the context class to setpublic void setInitializers(Collection<? extends ApplicationContextInitializer<?>> initializers)
ApplicationContextInitializer
that will be applied to the Spring
ApplicationContext
.initializers
- the initializers to setpublic void addInitializers(ApplicationContextInitializer<?>... initializers)
ApplicationContextInitializer
s to be applied to the Spring
ApplicationContext
.initializers
- the initializers to addpublic Set<ApplicationContextInitializer<?>> getInitializers()
ApplicationContextInitializer
s that
will be applied to the Spring ApplicationContext
.public void setListeners(Collection<? extends ApplicationListener<?>> listeners)
ApplicationListener
s that will be applied to the SpringApplication
and registered with the ApplicationContext
.listeners
- the listeners to setpublic void addListeners(ApplicationListener<?>... listeners)
ApplicationListener
s to be applied to the SpringApplication and
registered with the ApplicationContext
.listeners
- the listeners to addpublic Set<ApplicationListener<?>> getListeners()
ApplicationListener
s that will be
applied to the SpringApplication and registered with the ApplicationContext
.public static ConfigurableApplicationContext run(Object source, String... args)
SpringApplication
from the
specified source using default settings.source
- the source to loadargs
- the application arguments (usually passed from a Java main method)ApplicationContext
public static ConfigurableApplicationContext run(Object[] sources, String[] args)
SpringApplication
from the
specified sources using default settings and user supplied arguments.sources
- the sources to loadargs
- the application arguments (usually passed from a Java main method)ApplicationContext
public static void main(String[] args) throws Exception
Most developers will want to define their own main method can call the
run
method instead.
args
- command line argumentsException
- if the application cannot be startedrun(Object[], String[])
,
run(Object, String...)
public static int exit(ApplicationContext context, ExitCodeGenerator... exitCodeGenerators)
SpringApplication
and obtain a
code indicating success (0) or otherwise. Does not throw exceptions but should
print stack traces of any encountered. Applies the specified
ExitCodeGenerator
in addition to any Spring beans that implement
ExitCodeGenerator
. In the case of multiple exit codes the highest value
will be used (or if all values are negative, the lowest value will be used)context
- the context to close if possibleexitCodeGenerators
- exist code generatorsCopyright © 2015 Pivotal Software, Inc.. All rights reserved.