SpringBoot——启动方式及过程分析

xiaoxiao2021-02-28  103

         前篇文章简单的创建了一个Spring Boot的项目,了解到它可以以jar包的形式来运行,也可以通过main方法启动。Spring Boot有很多优点,使编码、配置、部署和监控等都变得简单。但是还是不知道它启动的原理。接下来具体分析一下。

 

SpringBoot的三种启动方式

1IDE运行Application这个类的main方法

        因为这是整个项目的入口类,它是一个artifactId+Application的入口类。@SpringBootApplication注解是整个SpringBoot的核心注解,它的目的就是开启Spring Boot的自动配置。这个注解的源码:

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters={@Filter( type=FilterType.CUSTOM, classes={TypeExcludeFilter.class} ),@Filter( type=FilterType.CUSTOM, classes={AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication { }

          我们可以看到它组合了@SpringBootConfiguration、@EnableAutoConfiguration以及@ComponentScan,我们在开发的过程中如果不使用@SpringBootApplication,则可以组合使用这三个注解。这三个注解中,@SpringBootConfiguration实际上就是我们前面几篇博客提到的@Configuration注解,表明这个类是一个配置类,@EnableAutoConfiguration则表示让SpringBoot根据类路径中的jar包依赖为当前项目进行自动配置,最后一个@ComponentScan的作用我也不赘述了,唯一要注意的是如果我们使用了@SpringBootApplication注解的话,系统会去入口类的同级包以及下级包中去扫描实体类,因此我们建议入口类的位置在groupId+arctifactID组合的包名下。

 

2、在SpringBoot的应用根目录下运行 mvn spring-boot:run

       这种方法是直接maven打包运行项目

 

3、使用mvn install生成jar后运行 

           先到项目目录下,进入target目录

   命令

  

mvn install cd target jave -jar xxx.jar

Spring Boot的启动过程

转载请注明原文地址: https://www.6miu.com/read-40250.html

最新回复(0)