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...
      } }
  6. Admin Features

    1. It is possible to enable admin-related features for the application by specifying the spring.application.admin.enabled property. This exposes the SpringApplicationAdminMXBean on the platform MBeanServer. You could use this feature to administer your Spring Boot application remotely. This feature could also be useful for any service wrapper implementation.
    2. If you want to know on which HTTP port the application is running, get the property with a key of local.server.port.
    3. Take care when enabling this feature, as the MBean exposes a method to shutdown the application.

  7. Application Exit

    1. Each SpringApplication registers a shutdown hook with the JVM to ensure that the ApplicationContext closes gracefully on exit. All the standard Spring lifecycle callbacks (such as the DisposableBean interface or the @PreDestroy annotation) can be used.

      In addition, beans may implement the org.springframework.boot.ExitCodeGenerator interface if they wish to return a specific exit code when SpringApplication.exit() is called. This exit code can then be passed to System.exit() to return it as a status code, as shown in the following example:

    2. Also, the ExitCodeGenerator interface may be implemented by exceptions. When such an exception is encountered, Spring Boot returns the exit code provided by the implemented getExitCode() method.
    3. @SpringBootApplication
      public class ExitCodeApplication { @Bean
      public ExitCodeGenerator exitCodeGenerator() {
      return () -> ;
      } public static void main(String[] args) {
      System.exit(SpringApplication
      .exit(SpringApplication.run(ExitCodeApplication.class, args)));
      } }

Spring boot 梳理 - SpringApplication的更多相关文章

  1. Spring Boot的SpringApplication类详解

    相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...

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

    Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootAppli ...

  3. Spring boot 梳理 - WebMvcConfigurer接口 使用案例

    转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,Vi ...

  4. Spring boot 梳理 - @Conditional

    @Conditional(TestCondition.class) 这句代码可以标注在类上面,表示该类下面的所有@Bean都会启用配置,也可以标注在方法上面,只是对该方法启用配置. spring框架还 ...

  5. Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

    @EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...

  6. Spring boot 梳理 - 模版引擎 -freemarker

    开发环境中关闭缓存 spring: thymeleaf: cache: false freemarker: cache: false Spring boot 集成 freemarker <dep ...

  7. Spring boot 梳理 - Spring boot 与 JSP

    若使用Spring boot 开发web应用中使用jsp,需要打包成war,并部署到非嵌入式servlet容器中运行,在嵌入式servlet中无法运行,且需要匹配非嵌入式servlet版本与Sprin ...

  8. Spring boot 梳理 - Spring boot自动注册DispatcherServlet

    spring boot提供的DispatcherServlet的name就是“dispatcherServlet”. 源码 public ServletRegistrationBean dispatc ...

  9. Spring boot - 梳理 - 根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置

    根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置

随机推荐

  1. jvm系列(七):如何优化Java GC「译」

    本文由CrowHawk翻译,地址:如何优化Java GC「译」,是Java GC调优的经典佳作. Sangmin Lee发表在Cubrid上的”Become a Java GC Expert”系列文章 ...

  2. 大转盘(CocosCreator)

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321 1.在场景中搭建大转盘场景,假设 奖项有n项,对应的每项旋转角度如下: 第几项 需要旋转的角度 0 360/n/2 1 360/ ...

  3. android下JNI开发

    android下JNI开发 what 什么是JNI JNI java native interface native本地 java本地接口 通过JNI可以实现java和本地代码之间相互调用 jni可以 ...

  4. 玩转 SpringBoot 2 快速整合 | 丝袜哥(Swagger)

    概述 首先让我引用 Swagger 官方的介绍: Design is the foundation of your API development. Swagger makes API design ...

  5. 根据图中的盲点坐标,弹出div层

    <div class="map_r" id="mapinfo" style="position: absolute; top: 20px; le ...

  6. 基于wanAndroid-项目实战

    # QzsWanAndroid - [基于 wanandroid.com 开发的 MVP + Retrofit2 + RxJava2 +okhttp3 开发的 Android APP](https:/ ...

  7. 步入vue.js世界

    一.遇见vue.js 1.1 Vue.js是什么? Vue.js 是一套用于构建用户界面的渐进式框架,Vue 的核心库只关注视图层,不仅易于上手,还便于与第三方库或既有项目整合.Vue.js通过简单的 ...

  8. c++学习笔记_4

    前言:本笔记所对应的课程为中国大学mooc中北京大学的程序设计与算法(三)C++面向对象程序设计,主要供自己复习使用,且本笔记建立在会使用c和java的基础上,只针对与c和java的不同来写 运算符重 ...

  9. set和push方法压入栈顶的值获取方法

    向值栈里面放数据(储存的位置在root域里面) 向值栈放数据有多种方式,往往我们只用其中一种 1.set方法压栈 1)在Action中获取值栈对象,使用set()方法向值栈存放数据 ActionCon ...

  10. 问题.beego路由设置及请求参数传递

    最近项目组安排将一组Lua实现的web服务端代码重构成Go实现,所以顺便学习了下Lua和Go,这里记录下在尝试重构的过程中遇到的几个问题. 1.beego路由设置 路由设置简单说下,主要是调用了pac ...