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:
- Create an appropriate
ApplicationContext instance (depending on your
classpath)
- Register a
CommandLinePropertySource to expose command line arguments as
Spring properties
- Refresh the application context, loading all singleton beans
- Trigger any
CommandLineRunner beans
In most circumstances the static
#run(Class,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) {
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
#getSources() from:
- The fully qualified class name to be loaded by
AnnotatedBeanDefinitionReader
- The location of an XML resource to be loaded by
XmlBeanDefinitionReader, or
a groovy script to be loaded by
GroovyBeanDefinitionReader
- The name of a package to be scanned by
ClassPathBeanDefinitionScanner
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").