Springboot 2使用SpringApplication

SpringApplication

Use the static method

SpringApplication.run(MySpringConfiguration.class, args);

Use the constructor

SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);

Use builder

new SpringApplicationBuilder(Application.class)
    .bannerMode(Banner.Mode.OFF)
    .run(args);

1, failure analyzer

Initialization failed to achieve the analyzer FailureAnalyzer interface, you can at the time failed to start printing the error log and resolve methods of operation. For example, start port is occupied when the log is printed as follows:

2019-08-07 10:22:32.534 ERROR 2616 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The Tomcat connector configured to listen on port 1111 failed to start. The port may already be in use or the connector may be misconfigured.

Action:

Verify the connector's configuration, identify and stop any process that's listening on port 1111, or configure this application to listen on another port.

2, Custom Banner

Banner.txt can be added to the class file path or file configuration spring.banner.location attribute specifies the path defined from Banner. If the file does not encode UTF-8, you can be configured to specify the encoding spring.banner.charset. In addition to text files, you can also add banner.gif, banner.jpg, banner.png picture file to the class path, or configure spring.banner.image.location specify the image path. After the picture is converted to ASCII graphics before printing banner.txt.

Variables may be added banner.txt, such as $ {spring-boot.version} Springboot obtain the current version.

Banner can be specified or configured by the encoding mode output:

Encoding: SpringApplication.setBanner(Mode.CONSOLE)or SpringApplicationBuilder.bannerMode(Mode.CONSOLE)
configuration file:spring.main.banner-mode=console

  • OFF: disable printing
  • CONSOLE: agree with System.out to the console
  • LOG: Print to a log file

3, application events and listeners

You can use SpringApplication.addListeners () or SpringApplicationBuilder.listeners () to register an event listener;
you can also create a file META-INF / spring.factories, key is org.springframework.context.ApplicationListener, value is a listener implementation class, examples are as follows;

org.springframework.context.ApplicationListener=com.example.project.MyListener

Send order application events

  1. ApplicationStartingEvent before sending in the application but have not started any treatment (except listeners and initializers)
  2. ApplicationEnvironmentPreparedEvent sent before the context of the needs of the environment Environment is known, create context
  3. Send refresh after ApplicationPreparedEvent before beginning to load bean definitions
  4. After the refresh is sent before ApplicationStartedEvent context, the calling application and a command line to run the program
  5. ApplicationReadyEvent sent after calling application and a command line to run the program. It indicates that the application is ready to service requests.
  6. ApplicationFailedEvent start sending abnormal occurrence

Listeners relevant principles see Spring Event Listeners Source

4, Web Environment

Specifies the web environment: SpringApplication.setWebApplicationType(WebApplicationType.SERVLET)orSpringApplicationBuilder.web(WebApplicationType.SERVLET)

WebApplicationType

  • NONE: non-web environment, the context of useAnnotationConfigApplicationContext
  • SERVLET: Spring MVC environment, the context of useAnnotationConfigServletWebServerApplicationContext
  • REACTIVE: Spring WebFlux environment, the context of useAnnotationConfigReactiveWebServerApplicationContext

5, specify the parameters args

Such as the use debug mode to run the program:java -jar myproject.jar --debug

6, ApplicationRunner and CommandLineRunner

Implementing these interfaces can be after the application starts to perform some operations, the default will be executed first ApplicationRunner.

7, exit the application

Each application will Springboot JVM to register a shutdown hook to ensure that the ApplicationContextnormally closed on exit. You can use all the standard Spring lifecycle callbacks (for example, DisposableBeanan interface or @PreDestroyannotation).

In addition, if the bean wish to call to SpringApplication.exit()return to a specific exit code, then you can implement org.springframework.boot.ExitCodeGeneratorthe interface. The code is then passed to the exit System.exit(), as follows:

@SpringBootApplication
public class ExitCodeApplication {

    @Bean
    public ExitCodeGenerator exitCodeGenerator() {
        return () -> 42;
    }

    public static void main(String[] args) {
        System.exit(SpringApplication.exit(SpringApplication.run(Bootstrap.class, args)));
    }

}

ExitCodeGeneratorThe interface can also be achieved by way of an exception. When such an exception occurs, Spring Boot returns achievable getExitCode()exit code methods.

8, management

By specifying spring.application.admin.enabled, you can enable management capabilities for application properties. This will MBeanServeropen the platform SpringApplicationAdminMXBean. You can use this feature to remotely manage Spring Boot application. This feature is achieved for any service wrapper is also useful.

Be careful enable this feature, because MBean discloses a method to close the application: Note.

Guess you like

Origin www.cnblogs.com/bigshark/p/11349557.html