Article From:

Original connection:

You can easily start a Spring application through the () method, for example.

public static void main(String[] args) {, args);

Running result

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: Spring Boot ::   v2.0.1.RELEASE

2013-07-31 00:08:16.117  INFO 56603 --- [           main]            : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb)
2013-07-31 00:08:16.166  INFO 56603 --- [           main] ationConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy
2014-03-04 13:09:54.912  INFO 41370 --- [           main] .t.TomcatServletWebServerFactory : Server initialized with port: 8080
2014-03-04 13:09:56.501  INFO 41370 --- [           main]            : Started SampleApplication in 2.992 seconds (JVM running for 3.658)

The default Log level is the INFO level.

Custom banner

bannerIt is printed when the Spring application starts. If you need to customize banner, you can add banner.txt to classpath, or set it up.spring.banner.locationThe properties set the path to the banner. The default encoding of banner is UTF-8. If you do not use UTF-8, you can set it up.spring.banner.charsetProperty sets its encoding. In addition to text files, you can also use banner.gif, banner.jpeg, banner.png and other image files as banner, or set upspring.banner.image.locationThe property set the picture banner. The picture has Spring to ASCII output. Add the following placeholder in banner.txt

  • ${application.version} stayMANIFEST.MFThe version number defined in the version, for exampleImplementation-Version: 1.0The version number is 1
  • ${application.formatted-version} Version number formatting plus V, such as v1.0
  • ${spring-boot.version} The version of Spring Boot, such as 2.0.1.RELEASE, is used.
  • ${spring-boot.formatted-version} Version number plus V, such as v2.0.1.RELEASE
  • ${Ansi.NAME},${AnsiColor.NAME} ,${AnsiBackground.NAME},${AnsiStyle.NAME} ANSI escape codename
  • ${application.title} MANIFEST.MF A defined application name, such asImplemention-Title: MyAppTake MyApp

Can also be realizedorg.springframework.boot.BannerInterfaceprintBanner()Method customize Banner and set banner with SpringApplication.setBanner (). To configurespring.main.banner-modeSet whether to display banner at the console

Custom SpringApplication

You can customize SpringApplication by creating custom SpringApplication examples, for example.

public static void main(String[] args) {
	SpringApplication app = new SpringApplication(MySpringConfiguration.class);

Customizing SpringApplication with builder

new SpringApplicationBuilder()
Application events and listeners

In addition to the usual Spring Framework events, such asContextRefreshedEventWait,SpringApplicationSome other application times will also be sent. Because some events were triggered before ApplicationContext was created, it is not possible to register listeners of these events by way of @Bean. You can use SpringApplication.addLIsteners () breaks the law or SpringApplicationBuilder.listeners () method registers listeners. It can also be added by addingMETA-INF/spring.factoriesFile to project, automatic registration listener, for exampleorg.springframework.context.ApplicationListener=come.example.project.MyListener After application, the application time is triggered in the following order, triggering the 1. application to start running.ApplicationStartingEventEvent 2. triggers before ApplicationContext is created, when Environment is available.ApplicationEnvironmentPreparedEventEvent。 3. bean definition after loading, refresh before triggeringApplicationPreparedEventAfter event 4. context refresh, command-line runner triggers before running.ApplicationStartedEvent 5. command-lie runnerAfter running, triggerApplicationReadyEventIndicates that the application can accept 6. of the requests.ApplicationFailedEvent The application event is sent through the event publishing mechanism of the Spring Framework, which ensures that the event sent to the child context will also be sent to its ancestor context. So if ApplicationContext has a hierarchyMany of the same events may be received. In order to distinguish these events, you need to apply their own ApplicationContext into the event, and judge whether the event is processed again when the event is received, and can be used to inherit.ApplicationContextAwareInjecting ApplicationContext, or if a listener is used, a bean can also be used to inject ApplicationContext into @Autowired.

WebEnvironmental Science

SpringApplicationYou can create the right according to the configurationApplicationContext

  • If Spring MVC is provided, then useAnnotationConfigServletWebServerApplicationContext
  • If Spring MVC is not provided, WebFlux is provided.AnnotationConfigReactiveWebApplicationContext
  • If not provided, useAnnotationConfigApplicationContext

You can also use setApplication (…) to set up ApplicationContext entirely.

Access application parameters

If you need to access the args parameter in (…), you can do this by beanVisit。 InterfaceApplicationArgumentsNot only does String[] provide access to native parameters, it also provides option and non-option parameters, for example.

import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*

public class MyBean {

	public MyBean(ApplicationArguments args) {
		boolean debug = args.containsOption("debug");
		List<String> files = args.getNonOptionArgs();
		// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]

Using ApplicationRunner and CommandLineRunner

If you need to run other code after running (), you can achieve it by implementing it.ApplicationRunnerperhapsCommandLineRunnerInterfaces, which function the same, provide a run () method, which is called after calling ().

import org.springframework.boot.*
import org.springframework.stereotype.*

public class MyBean implements CommandLineRunner {

	public void run(String... args) {
		// Do something...


If multiple CommandLineRunner or ApplicationRunner are defined, the interface can be provide the order.

Application exit

Each SpringApplication will register a JVM exit hook to ensure that ApplicationContext can be gracefully closed. In addition, bean can be implementedorg.springfamework.boot.ExitCodeGeneratorThe interface returns a specific return code when calling SpringApplication.exit (), which can be passed to System.exit (), for example:

public class ExitCodeApplication {

	public ExitCodeGenerator exitCodeGenerator() {
		return () -> 42;

	public static void main(String[] args) {
				.exit(, args)));


Leave a Reply

Your email address will not be published. Required fields are marked *