SpringBoot入门
视频学习地址:url:https://www.bilibili.com/video/BV1Et411Y7tQ?p=1
链接: link
1,pom文件
//该文件作为版本仲裁中心,对于我们未来使用的各种场景下的各个jar包的版本进行了约束,当我们在没有显示的声明我们的版本号的时候,就会使用springBoot为我们默认的版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
实际的版本声明在该pom文件中声明
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
2,spirng-boot-starter-*启动器的含义
eg:spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
以下为spring-boot-web中所添加的依赖,有了这些依赖,我们便不再需要使用本地的tomcat服务器,因为在我们的项目中将会引入我们所需要的tomcat依赖项(这大概也就是通常所说的springboot内置了Tomcat服务器的原因吧?)
当我们在不同的场景下,我们只需要导入对应的启动器就可以了,因为不同的启动器中已经有了我们所需要的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.3.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.3.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
3@springBootApplicaton注解详解
springBootApplicaton是一个复合注解,其中的两个核心注解如下
@SpringBootConfiguration
@EnableAutoConfiguration
-
@SpringBootConfiguration
@Configuration
将其标注为一个配置类@Component
将其放置在spring容器当中
-
@EnableAutoConfiguration(组合注解)
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
@AutoConfigurationPackage
@Import({Registrar.class})
在容器中添加了一个组件,而该组件的作用就是能够将带有@springbootApplication注解的包名获取到,并且能够将该包下的所有类进行扫描,对于需要交给spring管理的组件,会将其加入到容器当中
通过该图片可以看出,对于带有@SpringBootApplicaion注解的类的所在包已经获取到了。
2 .@Import({AutoConfigurationImportSelector.class})
(向容器中添加一些组件)如下展示了加载spring-boot-configure中的spring-factorys中的指定资源的核心逻辑。
springboot自动配置的核心