Class SpringApplication

  • public class SpringApplication
    extends Object
    Class 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(Class, String[]) method can be called directly from your main method to bootstrap your application:
     public class MyApplication  {
       // ... Bean definitions
       public static void main(String[] args) throws Exception {, 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 application = new SpringApplication(MyApplication.class);
       // ... customize application settings here
    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, you may also set sources from: Configuration properties are also bound to the SpringApplication. This makes it possible to set SpringApplication properties dynamically, like additional sources ("spring.main.sources" - a CSV list) the flag to indicate a web environment ("spring.main.web-application-type=none") or the flag to switch off the banner ("spring.main.banner-mode=off").
    Phillip Webb, Dave Syer, Andy Wilkinson, Christian Dupuis, Stephane Nicoll, Jeremy Rickard, Craig Burke, Michael Simons, Madhura Bhave, Brian Clozel, Ethan Rubinson
    See Also:
    run(Class, String[]), run(Class[], String[]), SpringApplication(Class...)
    • Field Detail


        public static final String DEFAULT_CONTEXT_CLASS
        The class name of application context that will be used by default for non-web environments.
        See Also:
        Constant Field Values

        public static final String DEFAULT_WEB_CONTEXT_CLASS
        The class name of application context that will be used by default for web environments.
        See Also:
        Constant Field Values

        public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS
        The class name of application context that will be used by default for reactive web environments.
        See Also:
        Constant Field Values

        public static final String BANNER_LOCATION_PROPERTY_VALUE
        Default banner location.
        See Also:
        Constant Field Values

        public static final String BANNER_LOCATION_PROPERTY
        Banner location property key.
        See Also:
        Constant Field Values
    • Method Detail

      • bindToSpringApplication

        protected void bindToSpringApplication(ConfigurableEnvironment environment)
        Bind the environment to the SpringApplication.
        environment - the environment to bind
      • postProcessApplicationContext

        protected void postProcessApplicationContext(ConfigurableApplicationContext context)
        Apply any relevant post processing the ApplicationContext. Subclasses can apply additional processing as required.
        context - the application context
      • logStartupInfo

        protected void logStartupInfo(boolean isRoot)
        Called to log startup information, subclasses may override to add additional logging.
        isRoot - true if this application is the root of a context hierarchy
      • logStartupProfileInfo

        protected void logStartupProfileInfo(ConfigurableApplicationContext context)
        Called to log active profile information.
        context - the application context
      • getApplicationLog

        protected Log getApplicationLog()
        Returns the Log for the application. By default will be deduced.
        the application log
      • load

        protected void load(ApplicationContext context,
                            Object[] sources)
        Load beans into the application context.
        context - the context to load beans into
        sources - the sources to load
      • getResourceLoader

        public ResourceLoader getResourceLoader()
        The ResourceLoader that will be used in the ApplicationContext.
        the resourceLoader the resource loader that will be used in the ApplicationContext (or null if the default)
      • getClassLoader

        public 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.
        a ClassLoader (never null)
      • refresh

        protected void refresh(ApplicationContext applicationContext)
        Refresh the underlying ApplicationContext.
        applicationContext - the application context to refresh
      • registerLoggedException

        protected void registerLoggedException(Throwable exception)
        Register that the given exception has been logged. By default, if the running in the main thread, this method will suppress additional printing of the stacktrace.
        exception - the exception that was logged
      • getMainApplicationClass

        public Class<?> getMainApplicationClass()
        Returns the main application class that has been deduced or explicitly configured.
        the main application class or null
      • 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.
        mainApplicationClass - the mainApplicationClass to set or null
      • getWebApplicationType

        public WebApplicationType getWebApplicationType()
        Returns the type of web application that is being run.
        the type of web application
      • setWebEnvironment

        public void setWebEnvironment(boolean webEnvironment)
        Deprecated. since 2.0.0 in favor of setWebApplicationType(WebApplicationType)
        Sets if this application is running within a web environment. If not specified will attempt to deduce the environment based on the classpath.
        webEnvironment - if the application is running in a web environment
      • setWebApplicationType

        public void setWebApplicationType(WebApplicationType webApplicationType)
        Sets the type of web application to be run. If not explicitly set the type of web application will be deduced based on the classpath.
        webApplicationType - the web application type
      • setHeadless

        public void setHeadless(boolean headless)
        Sets if the application is headless and should not instantiate AWT. Defaults to true to prevent java icons appearing.
        headless - if the application is headless
      • setRegisterShutdownHook

        public void setRegisterShutdownHook(boolean registerShutdownHook)
        Sets if the created 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 registered
      • setBanner

        public void setBanner(Banner banner)
        Sets the Banner instance which will be used to print the banner when no static banner file is provided.
        banner - the Banner instance to use
      • setBannerMode

        public void setBannerMode(Banner.Mode bannerMode)
        Sets the mode used to display the banner when the application runs. Defaults to Banner.Mode.CONSOLE.
        bannerMode - the mode used to display the banner
      • setLogStartupInfo

        public void setLogStartupInfo(boolean logStartupInfo)
        Sets if the application information should be logged when the application starts. Defaults to true.
        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.
        addCommandLineProperties - if command line arguments should be exposed
      • setDefaultProperties

        public void setDefaultProperties(Map<String,Object> defaultProperties)
        Set default environment properties which will be used in addition to those in the existing Environment.
        defaultProperties - the additional properties to set
      • setAdditionalProfiles

        public void setAdditionalProfiles(String... profiles)
        Set additional profile values to use (on top of those set in system or command line properties).
        profiles - the additional profiles to set
      • setBeanNameGenerator

        public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
        Sets the bean name generator that should be used when generating bean names.
        beanNameGenerator - the bean name generator
      • setEnvironment

        public void setEnvironment(ConfigurableEnvironment environment)
        Sets the underlying environment that should be used with the created application context.
        environment - the environment
      • getSources

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

        Sources set here will be used in addition to any primary sources set in the constructor.

        the application sources.
        See Also:
        SpringApplication(Class...), getAllSources()
      • setSources

        public void setSources(Set<String> sources)
        Set additional sources that will be used to create an ApplicationContext. A source can be: a class name, package name, or an XML resource location.

        Sources set here will be used in addition to any primary sources set in the constructor.

        sources - the application sources to set
        See Also:
        SpringApplication(Class...), getAllSources()
      • getAllSources

        public Set<Object> getAllSources()
        Return an immutable set of all the sources that will be added to an ApplicationContext when run(String...) is called. This method combines any primary sources specified in the constructor with any additional ones that have been explicitly set.
        an immutable set of all sources
      • setResourceLoader

        public void setResourceLoader(ResourceLoader resourceLoader)
        Sets the ResourceLoader that should be used when loading resources.
        resourceLoader - the resource loader
      • run

        public static ConfigurableApplicationContext run(Class<?>[] primarySources,
                                                         String[] args)
        Static helper that can be used to run a SpringApplication from the specified sources using default settings and user supplied arguments.
        primarySources - the primary sources to load
        args - the application arguments (usually passed from a Java main method)
        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 and call the run method instead.

        args - command line arguments
        Exception - if the application cannot be started
        See Also:
        run(Class[], String[]), run(Class, String...)
      • exit

        public 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. 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 possible
        exitCodeGenerators - exist code generators
        the outcome (0 if successful)