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

原文地址:https://www.cnblogs.com/jiangtao1218/p/10201483.html

时间: 2024-11-07 08:10:54

Spring boot 梳理 - SpringApplication的相关文章

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

Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootApplication 可能会有问题. Main类在包路径中的位置:强烈建议main类放在包的根路径上.We generally recommend that you locate your main application class in a root package above other classe

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

转:https://blog.csdn.net/isea533/article/details/50281151 Spring Boot 支持多种外部配置方式,这些方式优先级如下: 命令行参数 来自java:comp/env的JNDI属性 Java系统属性(System.getProperties()) 操作系统环境变量 RandomValuePropertySource配置的random.*属性值 jar包外部的application-{profile}.properties或applicat

(004)Spring Boot之SpringApplication.run为什么在不加注解的类中也可以运行,及其对比

一般来说springBoot初始运行的类上面会加SpringBootApplication注解,但是我们发现不加注解也可以成功运行,示例如下: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSche

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

转:https://yq.aliyun.com/articles/617307 SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,ViewResolver,MessageConverter,该怎么做呢.在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等.SpringBoot 2.0 后,该类被标记为@Deprecated.因此我们只能靠实现WebMvc

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

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

Spring boot 梳理 [email&#160;protected]、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented @Import({DelegatingWebMvcConfiguration.clas

Spring boot 梳理 - 显示Springboot默认自动生成的bean

@Autowired public ApplicationContext context; @Bean public ViewResolver freeMarkerViewResolver(){ String[] arr=context.getBeanDefinitionNames(); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } FreeMarkerViewResolver view=new FreeMarkerVi

翻译 Spring Boot How To

Spring Boot How To 1. 简介 本章节将回答一些常见的"我该怎么做"类型的问题,这些问题在我们使用Spring Boot时经常遇到.这绝不是一个详尽的列表,但它覆盖了很多方面. 如果遇到一个特殊的我们没有覆盖的问题,你可能想去查看stackoverflow.com 2. Spring Boot应用 2.1. 解决自动配置问题 Spring Boot自动配置总是尝试尽最大努力去做正确的事,但有时候会失败并且很难说出失败原因. 在每个Spring Boot Applica

[译]Spring Boot 构建一个RESTful Web服务

翻译地址:https://spring.io/guides/gs/rest-service/ 构建一个RESTful Web服务 本指南将指导您完成使用spring创建一个“hello world”RESTful Web服务的过程. 你将会构建什么 您将构建一个将接受HTTP GET请求的服务: 您将构建一个将接受HTTP GET请求的服务: http://localhost:8080/greeting 1 1 并且使用JSON的形式进行响应: {"id":1,"conten