- pom.xml文件
spring-boot-starter-parent 是当前项目的父级依赖
<!--当前项目的父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
- spring-boot-starter : Spring Boot 场景启动器,Spring Boot将所有的功能场景抽取出来,做成一个个的 starters(启动器),只需项目里引入相关场景的starter, 就会将它所有依赖导入进来。要用什么功能 就导入什么场景的启动器。(各种启动器可参见官方文档 starter)(官网网址:https://docs.spring.io/spring-boot/docs/2.2.4.RELEASE/reference/html/using-spring-boot.html#using-boot-starter)
- spring-boot-starter-parent :它父依赖 spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
spring-boot-dependencies 是管理了 Spring Boot项目中的所有依赖版本,下面截图只是一小部分
- 以后我们导入依赖默认不需要写版本号,也就是可以省去 version 标签;(当前没有在dependencies里 面管理的依赖自然需要声明版本号)
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 依赖导入了 Web 项目运行所依赖的组件。
2、引导类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
- 通常有一个名为 *Application 的入口 类,里面定义一个main方法,使用 SpringApplication.run(*Appliation.class, args); 来启动 SpringBoot 应用项目
- @SpringBootApplication注解主要组合了 @SpringBootConfiguration、@EnableAutoConfiguration、 @ComponentScan
- @SpringBootApplication注解说明:
- 标注在某个类上, 说明这个类是 Spring Boot 的引导类,Spring Boot 就应该运行这个类的main方法来 启动 SpringBoot 应用;
* * @author Phillip Webb * @author Stephane Nicoll * @since 1.2.0 */ @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 {
- @SpringBootApplication 是以下3个注解的总和:
- @SpringBootConfiguration:用于定义一个Spring Boot的配置类( 配置类 等同 配置文件) 引用了 @Configuration 注解,是Spring底层的一个注解,用于定义 Spring 的配置类。 配置类也是容器中的一个组件 @Component
-
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
-
@EnableAutoConfiguration : 告诉Spring Boot开启自动配置功能,这样Spring Boot会自动根据你导入的依赖jar包来自动 配置项目。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
- @AutoConfigurationPackage ——》 @Import({Registrar.class}) 会将引导类(@SpringBootApplication标注的类)所在的包及下面所有子包里面的所有 组件扫描到Spring容器;
-
@Import({AutoConfigurationImportSelector.class}) 将所有需要导入的组件以全类名的方式返回; 这些组件就会被添加到容器中 会给容器导入非常多的自动配置类(xxxxAutoConfiguration),就是导入并配置好当前项目 中所需要的组件,省去我们手动编写配置去注入组件.
Spring Boot在启动的时候从(spring-boot-autoconfigure-2.1.3.RELEASE.jar)类路径下的METAINF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自 动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的文件,自动配置类都帮我们完成了;
- @ComponentScan: 该注解标识的类, 会被 Spring 自动扫描并且装入bean容器
有不足之处,评论指出,多谢!!