org.springframework.boot
Class SpringApplication

java.lang.Object
  extended by org.springframework.boot.SpringApplication

public class SpringApplication
extends Object

Classes that can be used to bootstrap and launch a Spring application from a Java main method. By default class will perform the following steps to bootstrap your application:

In most circumstances the static run(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:

Author:
Phillip Webb, Dave Syer
See Also:
run(Object, String[]), run(Object[], String[]), SpringApplication(Object...)

Field Summary
static String DEFAULT_WEB_CONTEXT_CLASS
           
 
Constructor Summary
SpringApplication(Object... sources)
          Crate a new SpringApplication instance.
SpringApplication(org.springframework.core.io.ResourceLoader resourceLoader, Object... sources)
          Crate a new SpringApplication instance.
 
Method Summary
 void addInitializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
          Add ApplicationContextInitializers to be applied to the Spring 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.ApplicationContext 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 ApplicationContextInitializers 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.ApplicationContext 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.ApplicationContext run(Object[] sources, String[] args)
          Static helper that can be used to run a SpringApplication from the specified sources using default settings.
static org.springframework.context.ApplicationContext 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.ApplicationContext 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 setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
          Sets a Spring ApplicationContext that will be used for the application.
 void setApplicationContextClass(Class<? extends org.springframework.context.ApplicationContext> 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_WEB_CONTEXT_CLASS

public static final String DEFAULT_WEB_CONTEXT_CLASS
See Also:
Constant Field Values
Constructor Detail

SpringApplication

public SpringApplication(Object... sources)
Crate a new 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...).

Parameters:
sources - the bean sources
See Also:
run(Object, String[]), SpringApplication(ResourceLoader, Object...)

SpringApplication

public SpringApplication(org.springframework.core.io.ResourceLoader resourceLoader,
                         Object... sources)
Crate a new 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...).

Parameters:
resourceLoader - the resource loader to use
sources - the bean sources
See Also:
run(Object, String[]), SpringApplication(ResourceLoader, Object...)
Method Detail

getSpringFactoriesApplicationContextInitializers

protected Collection<org.springframework.context.ApplicationContextInitializer<?>> getSpringFactoriesApplicationContextInitializers()
Returns ApplicationContextInitializer loaded via the SpringFactoriesLoader. Subclasses can override this method to modify default initializers if necessary.


run

public org.springframework.context.ApplicationContext run(String... args)
Run the Spring application, creating and refreshing a new ApplicationContext.

Parameters:
args - the application arguments (usually passed from a Java main method)
Returns:
a running ApplicationContext

addPropertySources

protected void addPropertySources(org.springframework.core.env.ConfigurableEnvironment environment,
                                  String[] args)
Add any PropertySources to the environment.

Parameters:
environment - the environment
args - run arguments

printBanner

protected void printBanner()
Print a simple banner message to the console. Subclasses can override this method to provide additional or alternative banners.

See Also:
setShowBanner(boolean)

applyInitializers

protected void applyInitializers(org.springframework.context.ConfigurableApplicationContext context)
Apply any ApplicationContextInitializers to the context before it is refreshed.

Parameters:
context - the configured ApplicationContext (not refreshed yet)
See Also:
ConfigurableApplicationContext.refresh()

logStartupInfo

protected void logStartupInfo()
Called to log startup information, subclasses may override to add additional logging.


getApplicationLog

protected org.apache.commons.logging.Log getApplicationLog()
Returns the Log for the application. By default will be deduced.

Returns:
the application log

createApplicationContext

protected org.springframework.context.ApplicationContext createApplicationContext()
Strategy method used to create the ApplicationContext. By default this method will respect any explicitly set application context or application context class before falling back to a suitable default.

Returns:
the application context (not yet refreshed)
See Also:
setApplicationContext(ApplicationContext), setApplicationContextClass(Class)

postProcessApplicationContext

protected void postProcessApplicationContext(org.springframework.context.ApplicationContext context)
Apply any relevant post processing the ApplicationContext. Subclasses can apply additional processing as required.

Parameters:
context - the application context

load

protected void load(org.springframework.context.ApplicationContext context,
                    Object[] sources)
Load beans into the application context.

Parameters:
context - the context to load beans into
sources - the sources to load

createBeanDefinitionLoader

protected org.springframework.boot.BeanDefinitionLoader createBeanDefinitionLoader(org.springframework.beans.factory.support.BeanDefinitionRegistry registry,
                                                                                   Object[] sources)
Factory method used to create the BeanDefinitionLoader.

Parameters:
registry - the bean definition registry
sources - the sources to load
Returns:
the BeanDefinitionLoader that will be used to load beans

refresh

protected void refresh(org.springframework.context.ApplicationContext applicationContext)
Refresh the underlying ApplicationContext.

Parameters:
applicationContext - the application context to refresh

setMainApplicationClass

public void setMainApplicationClass(Class<?> mainApplicationClass)
Set a specific main application class that will be used as a log source and to obtain version information. By default the main application class will be deduced. Can be set to null if there is no explicit application class.

Parameters:
mainApplicationClass - the mainApplicationClass to set or null

setWebEnvironment

public void setWebEnvironment(boolean webEnvironment)
Sets if this application is running within a web environment. If not specified will attempt to deduce the environment based on the classpath.

Parameters:
webEnvironment - if the application is running in a web environment

setShowBanner

public void setShowBanner(boolean showBanner)
Sets if the Spring banner should be displayed when the application runs. Defaults to true.

Parameters:
showBanner - if the banner should be shown
See Also:
printBanner()

setLogStartupInfo

public void setLogStartupInfo(boolean logStartupInfo)
Sets if the application information should be logged when the application starts. Defaults to true

Parameters:
logStartupInfo - if startup info should be logged.

setAddCommandLineProperties

public void setAddCommandLineProperties(boolean addCommandLineProperties)
Sets if a CommandLinePropertySource should be added to the application context in order to expose arguments. Defaults to true.

Parameters:
addCommandLineProperties - if command line arguments should be exposed

setDefaultArgs

public void setDefaultArgs(String... defaultArgs)
Set default arguments which will be used in addition to those specified to the run methods. Default arguments can always be overridden by user defined arguments..

Parameters:
defaultArgs - the default args to set

setBeanNameGenerator

public void setBeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)
Sets the bean name generator that should be used when generating bean names.

Parameters:
beanNameGenerator - the bean name generator

setEnvironment

public void setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)
Sets the underlying environment that should be used with the created application context.

Parameters:
environment - the environment

getSources

public Set<Object> getSources()
Returns a mutable set of the sources that will be added to an ApplicationContext when run(String...) is called.

Returns:
the sources the application sources.
See Also:
SpringApplication(Object...)

setSources

public void setSources(Set<Object> sources)
The sources that will be used to create an ApplicationContext. A valid source is one of: a class, class name, package, package name, or an XML resource location. Can also be set using constructors and static convenience methods (e.g. run(Object[], String[])).

NOTE: sources defined here will be used in addition to any sources specified on construction.

Parameters:
sources - the sources to set
See Also:
SpringApplication(Object...)

setResourceLoader

public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
Sets the ResourceLoader that should be used when loading resources.

Parameters:
resourceLoader - the resource loader

setApplicationContextClass

public void setApplicationContextClass(Class<? extends org.springframework.context.ApplicationContext> applicationContextClass)
Sets the type of Spring 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.

Parameters:
applicationContextClass - the context class to set
See Also:
setApplicationContext(ApplicationContext)

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
Sets a Spring ApplicationContext that will be used for the application. If not specified an DEFAULT_WEB_CONTEXT_CLASS will be created for web based applications or an AnnotationConfigApplicationContext for non web based applications.

Parameters:
applicationContext - the spring application context.
See Also:
setApplicationContextClass(Class)

setInitializers

public void setInitializers(Collection<? extends org.springframework.context.ApplicationContextInitializer<?>> initializers)
Sets the ApplicationContextInitializer that will be applied to the Spring ApplicationContext. Any existing initializers will be replaced.

Parameters:
initializers - the initializers to set

addInitializers

public void addInitializers(org.springframework.context.ApplicationContextInitializer<?>... initializers)
Add ApplicationContextInitializers to be applied to the Spring ApplicationContext .

Parameters:
initializers - the initializers to add

getInitializers

public List<org.springframework.context.ApplicationContextInitializer<?>> getInitializers()
Returns a mutable list of the ApplicationContextInitializers that will be applied to the Spring ApplicationContext.

Returns:
the initializers

run

public static org.springframework.context.ApplicationContext run(Object source,
                                                                 String... args)
Static helper that can be used to run a SpringApplication from the specified source using default settings.

Parameters:
source - the source to load
args - the application arguments (usually passed from a Java main method)
Returns:
the running ApplicationContext

run

public static org.springframework.context.ApplicationContext run(Object[] sources,
                                                                 String[] args)
Static helper that can be used to run a SpringApplication from the specified sources using default settings.

Parameters:
sources - the sources to load
args - the application arguments (usually passed from a Java main method)
Returns:
the running ApplicationContext

main

public static void main(String[] args)
                 throws Exception
A basic main that can be used to launch an application. This method is useful when application sources are defined via a --spring.main.sources command line argument.

Most developers will want to define their own main method can call the run method instead.

Parameters:
args - command line arguments
Throws:
Exception
See Also:
run(Object[], String[]), run(Object, String...)

exit

public 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. 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)

Parameters:
context - the context to close if possible
exitCodeGenerators - exist code generators
Returns:
the outcome (0 if successful)


Copyright © 2013. All Rights Reserved.