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)
}
SpringApplications 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 AnnotatedBeanDefinitionReaderResource - An XML resource to be loaded by XmlBeanDefinitionReader,
or a groovy script to be loaded by GroovyBeanDefinitionReaderPackage - A Java package to be scanned by
ClassPathBeanDefinitionScannerCharSequence - 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
ApplicationContextInitializers to be applied to the Spring
ApplicationContext. |
void |
addListeners(org.springframework.context.ApplicationListener<?>... listeners)
Add
ApplicationListeners to be applied to the SpringApplication and
registered with the ApplicationContext. |
protected void |
addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment,
String[] args)
Add any
PropertySources to the environment. |
protected void |
applyInitializers(org.springframework.context.ConfigurableApplicationContext context)
Apply any
ApplicationContextInitializers 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. |
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<org.springframework.context.ApplicationContextInitializer<?>> |
getInitializers()
Returns readonly set of the
ApplicationContextInitializers that will be
applied to the Spring ApplicationContext. |
Set<org.springframework.context.ApplicationListener<?>> |
getListeners()
Returns readonly set of the
ApplicationListeners that will be applied to
the SpringApplication and registered with the ApplicationContext. |
org.springframework.core.io.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 |
handleError(org.springframework.context.ConfigurableApplicationContext context,
org.springframework.context.event.ApplicationEventMulticaster multicaster,
Throwable ex,
String... args) |
protected void |
load(org.springframework.context.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(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 |
setAdditionalProfiles(Collection<String> profiles)
Set additional profile values to use (on top of those set in system or command line
properties).
|
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 |
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(org.springframework.core.env.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 org.springframework.context.ApplicationContextInitializer<?>> initializers)
Sets the
ApplicationContextInitializer that will be applied to the Spring
ApplicationContext. |
void |
setListeners(Collection<? extends org.springframework.context.ApplicationListener<?>> listeners)
Sets the
ApplicationListeners 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 |
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...)public org.springframework.context.ConfigurableApplicationContext run(String... args)
ApplicationContext.args - the application arguments (usually passed from a Java main method)ApplicationContextprotected void handleError(org.springframework.context.ConfigurableApplicationContext context,
org.springframework.context.event.ApplicationEventMulticaster multicaster,
Throwable ex,
String... args)
protected void addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment,
String[] args)
PropertySources to the environment.environment - the environmentargs - run argumentsprotected void printBanner()
setShowBanner(boolean)protected void applyInitializers(org.springframework.context.ConfigurableApplicationContext context)
ApplicationContextInitializers 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 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 loadpublic org.springframework.core.io.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(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 nullpublic 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 setShowBanner(boolean showBanner)
true.showBanner - if the banner should be shownprintBanner()public void setLogStartupInfo(boolean logStartupInfo)
truelogStartupInfo - 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 Propertiespublic void setAdditionalProfiles(Collection<String> profiles)
profiles - the additional profiles 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. Any
initializers that are also ApplicationListener will be added to the
listeners automaticallyinitializers - the initializers to setpublic void addInitializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
ApplicationContextInitializers to be applied to the Spring
ApplicationContext. Any initializers that are also
ApplicationListener will be added to the
listeners automatically.initializers - the initializers to addpublic Set<org.springframework.context.ApplicationContextInitializer<?>> getInitializers()
ApplicationContextInitializers that will be
applied to the Spring ApplicationContext.public void setListeners(Collection<? extends org.springframework.context.ApplicationListener<?>> listeners)
ApplicationListeners that will be applied to the SpringApplication
and registered with the ApplicationContext. Any existing listeners will be
replaced. Any listeners that are also ApplicationContextInitializer will be
added to the initializers automatically.listeners - the listeners to setpublic void addListeners(org.springframework.context.ApplicationListener<?>... listeners)
ApplicationListeners to be applied to the SpringApplication and
registered with the ApplicationContext. Any listeners that are also
ApplicationContextInitializer will be added to the
initializers
automatically.listeners - the listeners to addpublic Set<org.springframework.context.ApplicationListener<?>> getListeners()
ApplicationListeners that will be applied to
the SpringApplication and registered with the 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)ApplicationContextpublic 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)ApplicationContextpublic 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 argumentsExceptionrun(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 © 2014. All rights reserved.