Spring boot 梳理 - SpringApplication

Posted 手握太阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot 梳理 - SpringApplication相关的知识,希望对你有一定的参考价值。

  1. 简单启动方式
    1. public static void main(String[] args) {
          SpringApplication.run(MySpringConfiguration.class, args);
      }

       

    2. 调试方式启动
      1. java -jar myproject-0.0.1-SNAPSHOT.jar --debug
  2. 高级启动方式
    1. @SpringBootApplication
      public class App 
      {
          public static void main( String[] args )
          {
              SpringApplication app=new SpringApplication(App.class);
              app.setBannerMode(Banner.Mode.OFF);
              app.run(args);
          }
      }

       

  3. Web Environment

    1. A SpringApplication attempts to create the right type of ApplicationContext on your behalf. The algorithm used to determine a WebApplicationType is fairly simple:

      • If Spring MVC is present, an AnnotationConfigServletWebServerApplicationContext is used
      • If Spring MVC is not present and Spring WebFlux is present, an AnnotationConfigReactiveWebServerApplicationContext is used
      • Otherwise, AnnotationConfigApplicationContext is used

      This means that if you are using Spring MVC and the new WebClient from Spring WebFlux in the same application, Spring MVC will be used by default. You can override that easily by calling setWebApplicationType(WebApplicationType).

      It is also possible to take complete control of the ApplicationContext type that is used by calling setApplicationContextClass(…?).

  4. Accessing Application Arguments

    1. If you need to access the application arguments that were passed to SpringApplication.run(…?), you can inject a org.springframework.boot.ApplicationArguments bean. The ApplicationArguments interface provides access to both the raw String[] arguments as well as parsed option and non-option arguments, as shown in the following example:
    2. import org.springframework.boot.*;
      import org.springframework.beans.factory.annotation.*;
      import org.springframework.stereotype.*;
      
      @Component
      public class MyBean {
      
          @Autowired
          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"]
          }
      
      }

       

  5. Using the ApplicationRunner or CommandLineRunner

    1. If you need to run some specific code once the SpringApplication has started, you can implement the ApplicationRunner or CommandLineRunner interfaces. Both interfaces work in the same way and offer a single run method, which is called just before SpringApplication.run(…?) completes.

      The CommandLineRunner interfaces provides access to application arguments as a simple string array, whereas the ApplicationRunner uses the ApplicationArguments interface discussed earlier. The following example shows a CommandLineRunner with a run method:

    2. If several CommandLineRunner or ApplicationRunner beans are defined that must be called in a specific order, you can additionally implement the org.springframework.core.Ordered interface or use the org.springframework.core.annotation.Order annotation.
    3. import org.springframework.boot.*;
      import org.springframework.stereotype.*;
      
      @Component
      public class MyBean implements CommandLineRunner {
      
          public void run(String... args) {
              // Do something...
          }
      
      }

       

以上是关于Spring boot 梳理 - SpringApplication的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot 梳理 - Spring Boot 属性配置和使用(转)

Spring boot 梳理 - SpringApplication

Spring boot 梳理 -@SpringBootApplication@EnableAutoConfiguration与(@EnableWebMVCWebMvcConfigurationSu(代

Spring Boot 梳理 - 4个核心

Spring boot 梳理 - 在bean中使用命令行参数-自动装配ApplicationArguments

Spring boot 梳理 - 代码结构(Main类的位置)