springboot梳理-springapplication

手握太阳      2022-02-14     133

关键词:

  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...
          }
      
      }

       

springboot源码解析-构建springapplication

1packagecom.microservice.framework;23importorg.springframework.boot.SpringApplication;4importorg.springframework.boot.autoconfigure.SpringBootApplication;56@SpringBootApplication7publicclassMySpringAp 查看详情

springboot—启动原理之springapplication对象的创建(代码片段)

创建SpringApplication对象SpringBoot版本为2.1.1.RELEASE@SpringBootApplicationpublicclassSpringbootDemoApplicationpublicstaticvoidmain(String[]args)SpringApplication.run(SpringbootDemoApplication.class,args) 查看详情

springapplication对象是如何构建的?springboot源码

注:该源码分析对应SpringBoot版本为2.1.0.RELEASE本篇接SpringBoot的启动流程是怎样的?SpringBoot源码(七)1温故而知新温故而知新,我们来简单回顾一下上篇的内容,上一篇我们分析了SpringBoot的启动流程,现将关键步骤再浓缩总结下... 查看详情

springboot:springapplication.run的源码解析(代码片段)

1.声明当前内容主要分析SpringBoot中的的SpringApplication的源码,一般来说Spring程序中的ioc的容器必定会创建,这个是SpringBoot的核心!2.基本的启动方式1.使用静态方法启动SpringApplication.run(primarySource,args)2.构建实例方式启动Sprin... 查看详情

springboot启动过程:springapplication及springapplicationbuilder

SpringBoot应用的启动是基于SpringApplication类的,下面一步步地分析。构造方法及初始化其构造方法有二,都调用了initialize方法,完成注解源的配置。 publicSpringApplication(Object...sources) initialize(sources); publicSpringApplication(R 查看详情

springboot项目中springapplication都做了什么

对springboot的自动装载注解有了一定了解后,我们还需要大致的知道,springapplication都做了什么呢?简单描述:1、实例化构造 2、执行run方法配置加载主类,大致概况如下几个方面,当然细化还有很多细节,我主要按我自己的... 查看详情

springboot启动流程

SpringBoot核心启动类的SpringApplication。从SpringApplication.run()开始先创建SpringApplication对象,并调用该对象的run方法。publicstaticConfigurableApplicationContextrun(Objectsource,String...args){returnrun(newObject[]{source}, 查看详情

springboot源码分析之springapplication构造方法核心源码分析(代码片段)

  前面给大家介绍了SpringBoot启动的核心流程,本文开始给大家详细的来介绍SpringBoot启动中的具体实现的相关细节。SpringApplication构造器  首先我们来看下在SpringApplication的构造方法中是如何帮我们完成这4个核心操作的。... 查看详情

springboot启动原理(基于2.x版本)-springapplication里有啥(代码片段)

...ication里的那些成员变量版本版本:3…0.1前两篇:SPRINGBOOT启动原理(基于2.x版本)(一)SPRINGBOOT启动原理(基于2.x版本)(二)-SpringFactoriesLoader引入基本上所有的springboot项目,在启动... 查看详情

springboot-运行部署

...的标准方法。我们的main方法SpringApplication通过调用委托给SpringBoot的类run。SpringApplication引导我们的应用程序,启动Spring,然后启动自动配置的TomcatWeb服务器。我们需要Example.class作为参数传递给run方法,以告诉SpringApplication哪个 查看详情

springboot启动过程:springapplication及springapplicationbuilder(代码片段)

SpringBoot应用的启动是基于SpringApplication类的,下面一步步地分析。构造方法及初始化其构造方法有二,都调用了initialize方法,完成注解源的配置。 publicSpringApplication(Object...sources) initialize(sources); publicSpringApplication(Res... 查看详情

springboot启动过程:springapplication及springapplicationbuilder(代码片段)

SpringBoot应用的启动是基于SpringApplication类的,下面一步步地分析。构造方法及初始化其构造方法有二,都调用了initialize方法,完成注解源的配置。 publicSpringApplication(Object...sources) initialize(sources); publicSpringApplication(Res... 查看详情

写一个springboot的入门案例,为啥springapplication.run会报错?

...技术B仔细看下你run的启动类,是不是把SpringApplication写成SpringBootApplication了 查看详情

#springboot常用扩展点介绍容器启动源码分析

SpringBoot常用扩展点介绍、容器启动源码分析SpringApplication.run()实例化一个SpringApplication创建一个SpringApplication实例publicstaticConfigurableApplicationContextrun(Class<?>[]primarySources,String[]args)returnnewSpringA 查看详情

涨姿势:springboot2.x启动全过程源码分析

目录SpringApplication实例run方法运行过程总结上篇《SpringBoot2.x启动全过程源码分析(一)入口类剖析》我们分析了SpringBoot入口类SpringApplication的源码,并知道了其构造原理,这篇我们继续往下面分析其核心run方法。SpringApplication实... 查看详情

springboot启动方式

1、系统自动生成SpringApplication.run(XX.class,args);2、创建SpringApplication对象SpringApplicationapp=newSpringApplication(DemoApplication.class);app.setBannerMode(Banner.Mode.OFF);app.run();3、FluentBuilderAPI构造者模 查看详情

springboot核心技术梳理(持续更新)

SpringBoot自动装配SpringBoot自动配置原理 查看详情

springboot是如何动起来的

SpringBoot是如何动起来的程序入口SpringApplication.run(BeautyApplication.class,args);执行此方法来加载整个SpringBoot的环境。1.从哪儿开始?SpringApplication.java/***RuntheSpringapplication,creatingandrefreshinganew*{@linkApplicationCo 查看详情