SpringApplication

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 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) {
    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) {
  SpringApplication application = new SpringApplication(MyApplication.class);
  // ... customize application settings here
  application.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, 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").

Author

Phillip Webb

Dave Syer

Andy Wilkinson

Christian Dupuis

Stephane Nicoll

Jeremy Rickard

Craig Burke

Michael Simons

Madhura Bhave

Brian Clozel

Ethan Rubinson

Chris Bono

Moritz Halbritter

Tadaya Tsuyukubo

Lasse Wulff

Yanming Zhou

Since

1.0.0

See also

Constructors

Link copied to clipboard
constructor(primarySources: Array<Class<out Any>>)
Create a new SpringApplication instance.
constructor(resourceLoader: ResourceLoader, primarySources: Array<Class<out Any>>)
Create a new SpringApplication instance.

Types

Link copied to clipboard
Exception that can be thrown to silently exit a running SpringApplication without handling run failures.
Link copied to clipboard
open class Augmented
Used to configure and run an augmented SpringApplication where additional configuration should be applied.
Link copied to clipboard
interface Running
Provides access to details of a SpringApplication run using run.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val BANNER_LOCATION_PROPERTY: String = "spring.banner.location"
Banner location property key.
Link copied to clipboard
Default banner location.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open var sources: Set<String>
Link copied to clipboard

Functions

Link copied to clipboard
open fun addBootstrapRegistryInitializer(bootstrapRegistryInitializer: BootstrapRegistryInitializer)
Adds BootstrapRegistryInitializer instances that can be used to initialize the BootstrapRegistry.
Link copied to clipboard
Add ApplicationContextInitializers to be applied to the Spring ApplicationContext.
Link copied to clipboard
open fun addListeners(listeners: Array<ApplicationListener<out Any>>)
Add ApplicationListeners to be applied to the SpringApplication and registered with the ApplicationContext.
Link copied to clipboard
open fun addPrimarySources(additionalPrimarySources: Collection<Class<out Any>>)
Add additional items to the primary sources that will be added to an ApplicationContext when run is called.
Link copied to clipboard
open fun exit(context: ApplicationContext, exitCodeGenerators: Array<ExitCodeGenerator>): Int
Static helper that can be used to exit a SpringApplication and obtain a code indicating success (0) or otherwise.
Link copied to clipboard
Create an application from an existing main method that can run with additional @Configuration or bean classes.
Link copied to clipboard
open fun getAllSources(): Set<Any>
Return an immutable set of all the sources that will be added to an ApplicationContext when run is called.
Link copied to clipboard
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.
Link copied to clipboard
Returns read-only ordered Set of the ApplicationContextInitializers that will be applied to the Spring ApplicationContext.
Link copied to clipboard
Returns read-only ordered Set of the ApplicationListeners that will be applied to the SpringApplication and registered with the ApplicationContext .
Link copied to clipboard
Return a SpringApplicationShutdownHandlers instance that can be used to add or remove handlers that perform actions before the JVM is shutdown.
Link copied to clipboard
open fun isKeepAlive(): Boolean
Whether to keep the application alive even if there are no more non-daemon threads.
Link copied to clipboard
open fun main(args: Array<String>)
A basic main that can be used to launch an application.
Link copied to clipboard
Run the Spring application, creating and refreshing a new ApplicationContext.
open fun run(primarySource: Class<out Any>, args: Array<String>): ConfigurableApplicationContext
Static helper that can be used to run a SpringApplication from the specified source using default settings.
open fun run(primarySources: Array<Class<out Any>>, args: Array<String>): ConfigurableApplicationContext
Static helper that can be used to run a SpringApplication from the specified sources using default settings and user supplied arguments.
Link copied to clipboard
open fun setAddCommandLineProperties(addCommandLineProperties: Boolean)
Sets if a CommandLinePropertySource should be added to the application context in order to expose arguments.
Link copied to clipboard
open fun setAddConversionService(addConversionService: Boolean)
Sets if the ApplicationConversionService should be added to the application context's Environment.
Link copied to clipboard
open fun setAdditionalProfiles(profiles: Array<String>)
Set additional profile values to use (on top of those set in system or command line properties).
Link copied to clipboard
open fun setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding: Boolean)
Sets if bean definition overriding, by registering a definition with the same name as an existing definition, should be allowed.
Link copied to clipboard
open fun setAllowCircularReferences(allowCircularReferences: Boolean)
Sets whether to allow circular references between beans and automatically try to resolve them.
Link copied to clipboard
open fun setApplicationContextFactory(applicationContextFactory: ApplicationContextFactory)
Sets the factory that will be called to create the application context.
Link copied to clipboard
open fun setBanner(banner: Banner)
Sets the Banner instance which will be used to print the banner when no static banner file is provided.
Link copied to clipboard
open fun setBannerMode(bannerMode: Banner.Mode)
Sets the mode used to display the banner when the application runs.
Link copied to clipboard
open fun setBeanNameGenerator(beanNameGenerator: BeanNameGenerator)
Sets the bean name generator that should be used when generating bean names.
Link copied to clipboard
open fun setDefaultProperties(defaultProperties: Map<String, Any>)
Set default environment properties which will be used in addition to those in the existing Environment.
open fun setDefaultProperties(defaultProperties: Properties)
Convenient alternative to setDefaultProperties.
Link copied to clipboard
Sets the underlying environment that should be used with the created application context.
Link copied to clipboard
open fun setHeadless(headless: Boolean)
Sets if the application is headless and should not instantiate AWT.
Link copied to clipboard
Sets the ApplicationContextInitializer that will be applied to the Spring ApplicationContext.
Link copied to clipboard
open fun setKeepAlive(keepAlive: Boolean)
Set whether to keep the application alive even if there are no more non-daemon threads.
Link copied to clipboard
open fun setLazyInitialization(lazyInitialization: Boolean)
Sets if beans should be initialized lazily.
Link copied to clipboard
open fun setListeners(listeners: Collection<out ApplicationListener<out Any>>)
Sets the ApplicationListeners that will be applied to the SpringApplication and registered with the ApplicationContext.
Link copied to clipboard
open fun setLogStartupInfo(logStartupInfo: Boolean)
Sets if the application information should be logged when the application starts.
Link copied to clipboard
open fun setRegisterShutdownHook(registerShutdownHook: Boolean)
Sets if the created ApplicationContext should have a shutdown hook registered.
Link copied to clipboard
open fun withHook(hook: SpringApplicationHook, action: Runnable)
open fun <T> withHook(hook: SpringApplicationHook, action: ThrowingSupplier<T>): T
Perform the given action with the given SpringApplicationHook attached if the action triggers an application run.