Core starter providing fundamental building blocks for Spring Boot applications with auto-configuration support, logging, and YAML parsing
—
Core application startup, lifecycle management, and command line argument processing functionality that forms the foundation of every Spring Boot application.
The main class for bootstrapping and launching a Spring application from a Java main method.
/**
* 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:
* - Create an ApplicationContext instance
* - Register a CommandLinePropertySource to expose command line arguments
* - Refresh the ApplicationContext
* - Trigger any CommandLineRunner beans
*/
public class SpringApplication {
/**
* Static convenience method to run a SpringApplication from a main method
* @param primarySource the primary source to load
* @param args the application arguments
* @return the running ApplicationContext
*/
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args);
/**
* Static convenience method to run a SpringApplication from a main method
* @param primarySources the primary sources to load
* @param args the application arguments
* @return the running ApplicationContext
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String... args);
/**
* Create a new SpringApplication instance
* @param primarySources the primary bean sources
*/
public SpringApplication(Class<?>... primarySources);
/**
* Create a new SpringApplication instance with a ResourceLoader
* @param resourceLoader the resource loader to use
* @param primarySources the primary bean sources
*/
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources);
/**
* Run the Spring application
* @param args the application arguments
* @return the running ApplicationContext
*/
public ConfigurableApplicationContext run(String... args);
/**
* Set the banner to be displayed when the application runs
* @param banner the banner to display
*/
public void setBanner(Banner banner);
/**
* Set the mode used to display the banner when the application runs
* @param bannerMode the mode to use
*/
public void setBannerMode(Banner.Mode bannerMode);
/**
* Set additional profile values to use
* @param profiles the profiles to add
*/
public void setAdditionalProfiles(String... profiles);
/**
* Set the type of web application
* @param webApplicationType the web application type
*/
public void setWebApplicationType(WebApplicationType webApplicationType);
/**
* Add ApplicationContextInitializer instances
* @param initializers the initializers to add
*/
public void addInitializers(ApplicationContextInitializer<?>... initializers);
/**
* Add ApplicationListener instances
* @param listeners the listeners to add
*/
public void addListeners(ApplicationListener<?>... listeners);
/**
* Return the additional profiles to use
* @return the additional profiles (never null)
*/
public Set<String> getAdditionalProfiles();
/**
* Set whether to allow bean definition overriding
* @param allowBeanDefinitionOverriding whether to allow overriding
*/
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding);
/**
* Set whether to allow circular references between beans
* @param allowCircularReferences whether to allow circular references
*/
public void setAllowCircularReferences(boolean allowCircularReferences);
/**
* Set whether to enable lazy initialization
* @param lazyInitialization whether to enable lazy initialization
*/
public void setLazyInitialization(boolean lazyInitialization);
/**
* Set whether the JVM should be kept alive after the context is closed
* @param keepAlive whether to keep the JVM alive
*/
public void setKeepAlive(boolean keepAlive);
/**
* Return whether the JVM should be kept alive after the context is closed
* @return true if the JVM should be kept alive
*/
public boolean isKeepAlive();
/**
* Set the ApplicationStartup to use for collecting startup metrics
* @param applicationStartup the startup metrics collector
*/
public void setApplicationStartup(ApplicationStartup applicationStartup);
/**
* Return the ApplicationStartup used for collecting startup metrics
* @return the startup metrics collector
*/
public ApplicationStartup getApplicationStartup();
/**
* Return the shutdown handlers that can be used to register shutdown hooks
* @return the shutdown handlers
*/
public static SpringApplicationShutdownHandlers getShutdownHandlers();
}Usage Examples:
// Simple application startup
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
// Custom SpringApplication configuration
@SpringBootApplication
public class CustomApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(CustomApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.setWebApplicationType(WebApplicationType.NONE);
app.run(args);
}
}Meta-annotations that configure the basic structure of a Spring Boot application.
/**
* Meta-annotation that combines @Configuration, @EnableAutoConfiguration,
* and @ComponentScan with sensible defaults
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface SpringBootApplication {
/**
* Exclude specific auto-configuration classes
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names
* @return the class names to exclude
*/
String[] excludeName() default {};
/**
* Base packages to scan for components
* @return the base packages
*/
String[] scanBasePackages() default {};
/**
* Base package classes to scan for components
* @return the base package classes
*/
Class<?>[] scanBasePackageClasses() default {};
/**
* Whether to proxy @Bean methods to enforce bean lifecycle behavior
* @return true if bean methods should be proxied
*/
boolean proxyBeanMethods() default true;
/**
* The BeanNameGenerator class to be used for naming detected components
* @return the BeanNameGenerator class
*/
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
}
/**
* Indicates that a class provides Spring Boot application configuration
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface SpringBootConfiguration {
/**
* Whether to proxy @Bean methods to enforce bean lifecycle behavior
* @return true if bean methods should be proxied
*/
boolean proxyBeanMethods() default true;
}Interface for accessing command line arguments in a structured way.
/**
* Provides access to the arguments that were used to run a SpringApplication
*/
public interface ApplicationArguments {
/**
* Return the raw unprocessed arguments that were passed to the application
* @return the source arguments
*/
String[] getSourceArgs();
/**
* Return the names of all option arguments
* @return the option names
*/
Set<String> getOptionNames();
/**
* Return whether the named option is present
* @param name the name to check
* @return true if the option is present
*/
boolean containsOption(String name);
/**
* Return the collection of values associated with the arguments option
* @param name the name to check
* @return a list of values (never null)
*/
List<String> getOptionValues(String name);
/**
* Return the collection of non-option arguments parsed
* @return the non-option arguments (never null)
*/
List<String> getNonOptionArgs();
}
/**
* Default implementation of ApplicationArguments
*/
public class DefaultApplicationArguments implements ApplicationArguments {
/**
* Create a new DefaultApplicationArguments instance
* @param args the source arguments
*/
public DefaultApplicationArguments(String... args);
}Interfaces for executing code after the Spring application has started.
/**
* Interface used to indicate that a bean should run when it is contained
* within a SpringApplication. Multiple CommandLineRunner beans can be
* defined within the same application context and can be ordered using
* the Ordered interface or @Order annotation.
*/
@FunctionalInterface
public interface CommandLineRunner {
/**
* Callback used to run the bean
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception;
}
/**
* Interface used to indicate that a bean should run when it is contained
* within a SpringApplication. Multiple ApplicationRunner beans can be
* defined within the same application context and can be ordered using
* the Ordered interface or @Order annotation.
*/
@FunctionalInterface
public interface ApplicationRunner {
/**
* Callback used to run the bean
* @param args incoming application arguments
* @throws Exception on error
*/
void run(ApplicationArguments args) throws Exception;
}Usage Examples:
@Component
public class DataSetupRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Setting up initial data...");
// Setup logic here
}
}
@Component
@Order(1)
public class PriorityRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
if (args.containsOption("setup")) {
System.out.println("Running priority setup...");
}
}
}Factories for creating appropriate ApplicationContext instances based on application type.
/**
* Strategy interface for creating the ApplicationContext used by a SpringApplication
*/
@FunctionalInterface
public interface ApplicationContextFactory {
/**
* Creates the ApplicationContext for the given application type
* @param webApplicationType the web application type
* @return a new ApplicationContext instance
*/
ConfigurableApplicationContext create(WebApplicationType webApplicationType);
/**
* Default factory implementation
*/
ApplicationContextFactory DEFAULT = new DefaultApplicationContextFactory();
}
/**
* Default implementation of ApplicationContextFactory
*/
public class DefaultApplicationContextFactory implements ApplicationContextFactory {
@Override
public ConfigurableApplicationContext create(WebApplicationType webApplicationType);
}Bootstrap-time registry for sharing objects between ApplicationContextInitializers and other bootstrap code.
/**
* A simple bootstrap context that is available during startup and Environment
* post-processing up to the point that the ApplicationContext is prepared
*/
public interface BootstrapContext {
/**
* Return an instance from the registry
* @param type the instance type
* @return the instance
* @throws IllegalStateException if the type is not registered
*/
<T> T get(Class<T> type) throws IllegalStateException;
/**
* Return an instance from the registry if available
* @param type the instance type
* @return the instance or null
*/
<T> T getOrElse(Class<T> type, T other);
/**
* Return if a registration exists for the given type
* @param type the instance type
* @return true if registered
*/
<T> boolean isRegistered(Class<T> type);
}
/**
* A configurable bootstrap context that is available during startup and
* Environment post-processing up to the point that the ApplicationContext is prepared
*/
public interface ConfigurableBootstrapContext extends BootstrapContext {
/**
* Register a specific type with the registry
* @param type the instance type
* @param instanceSupplier the instance supplier
*/
<T> void register(Class<T> type, InstanceSupplier<T> instanceSupplier);
/**
* Register a specific type with the registry if not already present
* @param type the instance type
* @param instanceSupplier the instance supplier
*/
<T> void registerIfAbsent(Class<T> type, InstanceSupplier<T> instanceSupplier);
/**
* Add a BootstrapContextClosedEvent listener
* @param listener the listener to add
*/
void addCloseListener(ApplicationListener<BootstrapContextClosedEvent> listener);
}
/**
* Supplier that can be used to create an instance when needed
*/
@FunctionalInterface
public interface InstanceSupplier<T> {
/**
* Factory method to create the instance
* @param context the bootstrap context
* @return the instance
*/
T get(BootstrapContext context);
}Enumeration for different types of web applications supported by Spring Boot.
/**
* An enumeration of possible types of web application
*/
public enum WebApplicationType {
/**
* The application is not a web application and should not start an embedded web server
*/
NONE,
/**
* The application is a servlet-based web application and should start an embedded servlet web server
*/
SERVLET,
/**
* The application is a reactive web application and should start an embedded reactive web server
*/
REACTIVE;
/**
* Determine the web application type based on the classpath
* @return the web application type
*/
static WebApplicationType deduceFromClasspath();
}Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter