springboot的核心思想:起步依赖&自动配置
Springboot整合mybatis
<dependencies>
<!-- mybatis的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
spring: #配置数据源
datasource:
url: jdbc:mysql://127.0.0.1:3306/db_stu?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
自动配置
启动类的springbootApplication注解
实现自动装配的注解ennableautoconfiguration
在点开有一个注解import去加载(autoconfigurationimportselector.class)
从这个类中可以看到getautoconfiguarations方法
通过通过加载META-INF/spring.factories了众多配置类(任何一个架包下面都有META-inf/spring.factories),像spring的aop,加载后去重排除,加载。
springbootconfiguration这个注解就是为什么bean能生效的原因,底层是configuration
autoconfigurationpackage 用于扫描启动类所属的包,将包下所有子包的所有主键扫描到ioc容器
1.3 介绍Spring Boot的启动流程
参考答案
启动流程
springboot启动流程主要分为三个部分:
第一部分进行SpringApplication的初始化模块,配置一些基本的环境变量、资源、构造器、监听器,
第二部分实现了应用具体的启动方案,包括启动流程的监听模块、加载配置环境模块、及核心的创建上下文环境模块,
第三部分是自动化配置模块,该模块作为springboot自动配置核心,在下面的启动程序中我们会串联起结构中的主要功能。
具体如下:
每个SpringBoot程序都有一个主入口,也就是main方法,main里面调用SpringApplication.run()启动整个spring-boot程序,该方法所在类需要使用@SpringBootApplication注解,以及@ImportResource注解(if need),
@SpringBootApplication包括三个注解,功能如下:
@EnableAutoConfiguration:SpringBoot根据应用所声明的依赖来对Spring框架进行自动配置
@SpringBootConfiguration(内部为@Configuration):被标注的类等于在spring的XML配置文件中(applicationContext.xml),装配所有bean事务,提供了一个spring的上下文环境
@ComponentScan:组件扫描,可自动发现和装配Bean,默认扫描SpringApplication的run方法里的Booter.class所在的包路径下文件,所以最好将该启动类放到根包路径下