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
- A XML resource to be loaded by XmlBeanDefinitionReader
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)
Crate a new
SpringApplication instance. |
SpringApplication(org.springframework.core.io.ResourceLoader resourceLoader,
Object... sources)
Crate a new
SpringApplication instance. |
Modifier and Type | Method and Description |
---|---|
void |
addInitializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
Add
ApplicationContextInitializer s to be applied to the Spring
ApplicationContext . |
protected void |
addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment,
String[] args)
Add any
PropertySource s to the environment. |
protected void |
applyInitializers(org.springframework.context.ConfigurableApplicationContext context)
Apply any
ApplicationContextInitializer s to the context before it is
refreshed. |
protected org.springframework.context.ConfigurableApplicationContext |
createApplicationContext()
Strategy method used to create the
ApplicationContext . |
protected org.springframework.boot.BeanDefinitionLoader |
createBeanDefinitionLoader(org.springframework.beans.factory.support.BeanDefinitionRegistry registry,
Object[] sources)
Factory method used to create the
BeanDefinitionLoader . |
static int |
exit(org.springframework.context.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. |
List<org.springframework.context.ApplicationContextInitializer<?>> |
getInitializers()
Returns a mutable list of the
ApplicationContextInitializer s that will be
applied to the Spring ApplicationContext . |
Set<Object> |
getSources()
Returns a mutable set of the sources that will be added to an ApplicationContext
when
run(String...) is called. |
protected Collection<org.springframework.context.ApplicationContextInitializer<?>> |
getSpringFactoriesApplicationContextInitializers()
Returns
ApplicationContextInitializer loaded via the
SpringFactoriesLoader . |
protected void |
load(org.springframework.context.ApplicationContext context,
Object[] sources)
Load beans into the application context.
|
protected void |
logStartupInfo()
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(org.springframework.context.ConfigurableApplicationContext context)
Apply any relevant post processing the
ApplicationContext . |
protected void |
printBanner()
Print a simple banner message to the console.
|
protected void |
refresh(org.springframework.context.ApplicationContext applicationContext)
Refresh the underlying
ApplicationContext . |
static org.springframework.context.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 org.springframework.context.ConfigurableApplicationContext |
run(Object source,
String... args)
Static helper that can be used to run a
SpringApplication from the
specified source using default settings. |
org.springframework.context.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 |
setApplicationContextClass(Class<? extends org.springframework.context.ConfigurableApplicationContext> applicationContextClass)
Sets the type of Spring
ApplicationContext that will be created. |
void |
setBeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)
Sets the bean name generator that should be used when generating bean names.
|
void |
setDefaultArgs(String... defaultArgs)
Set default arguments which will be used in addition to those specified to the
run methods. |
void |
setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)
Sets the underlying environment that should be used with the created application
context.
|
void |
setInitializers(Collection<? extends org.springframework.context.ApplicationContextInitializer<?>> initializers)
Sets the
ApplicationContextInitializer that will be applied to the Spring
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 |
setResourceLoader(org.springframework.core.io.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(org.springframework.core.io.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...)
protected Collection<org.springframework.context.ApplicationContextInitializer<?>> getSpringFactoriesApplicationContextInitializers()
ApplicationContextInitializer
loaded via the
SpringFactoriesLoader
. Subclasses can override this method to modify
default initializers if necessary.public org.springframework.context.ConfigurableApplicationContext run(String... args)
ApplicationContext
.args
- the application arguments (usually passed from a Java main method)ApplicationContext
protected void addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment, String[] args)
PropertySource
s to the environment.environment
- the environmentargs
- run argumentsprotected void printBanner()
setShowBanner(boolean)
protected void applyInitializers(org.springframework.context.ConfigurableApplicationContext context)
ApplicationContextInitializer
s to the context before it is
refreshed.context
- the configured ApplicationContext (not refreshed yet)ConfigurableApplicationContext.refresh()
protected void logStartupInfo()
protected org.apache.commons.logging.Log getApplicationLog()
Log
for the application. By default will be deduced.protected org.springframework.context.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(org.springframework.context.ConfigurableApplicationContext context)
ApplicationContext
. Subclasses can
apply additional processing as required.context
- the application contextprotected void load(org.springframework.context.ApplicationContext context, Object[] sources)
context
- the context to load beans intosources
- the sources to loadprotected org.springframework.boot.BeanDefinitionLoader createBeanDefinitionLoader(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, Object[] sources)
BeanDefinitionLoader
.registry
- the bean definition registrysources
- the sources to loadBeanDefinitionLoader
that will be used to load beansprotected void refresh(org.springframework.context.ApplicationContext applicationContext)
ApplicationContext
.applicationContext
- the application context to refreshpublic 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 setShowBanner(boolean showBanner)
true
.showBanner
- if the banner should be shownprintBanner()
public 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 setDefaultArgs(String... defaultArgs)
run
methods. Default arguments can always be overridden by user defined
arguments..defaultArgs
- the default args to setpublic void setBeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)
beanNameGenerator
- the bean name generatorpublic void setEnvironment(org.springframework.core.env.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(org.springframework.core.io.ResourceLoader resourceLoader)
ResourceLoader
that should be used when loading resources.resourceLoader
- the resource loaderpublic void setApplicationContextClass(Class<? extends org.springframework.context.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 org.springframework.context.ApplicationContextInitializer<?>> initializers)
ApplicationContextInitializer
that will be applied to the Spring
ApplicationContext
. Any existing initializers will be replaced. The default
initializers (from META-INF/spring.factories
are always appended at
runtime).initializers
- the initializers to setpublic void addInitializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
ApplicationContextInitializer
s to be applied to the Spring
ApplicationContext
.initializers
- the initializers to addpublic List<org.springframework.context.ApplicationContextInitializer<?>> getInitializers()
ApplicationContextInitializer
s that will be
applied to the Spring ApplicationContext
.public static org.springframework.context.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 org.springframework.context.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
run(Object[], String[])
,
run(Object, String...)
public static int exit(org.springframework.context.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 © 2013. All rights reserved.