版本:Spring Boot:已经吃了很多关于版本的亏!看之前先看版本信息。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
SpringBoot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Spring容器。该文件的位置如图:
源码分析:
一、从入口开始跟进
二、跟进run()方法
三、只执行了一个run()方法,继续跟进
四、可以看到只创建了一个对象,我们跟进构造函数
五、看到他调用的是this方法,其实就是下边的构造方法2
六、在构造中有一个方法三
七、跟进getSpringFactoriesInstances()
八、欢呼雀跃吧,loadFactoryNames()就是加载文件的方法
loadFactoryNames方法的作用是把/spring.factories文件中的配置类转化为对象,我们可以看到SpringApplication对象实例化时就是在这里加载META-INF/spring.factories文件
九、如果我们想排除一个自动引入的jar可以
十、查看Spring Boot自带的自动配置的包: spring-boot-autoconfigure-1.5.6.RELEASE.jar,打开其中的META-INF/spring.factories文件会找到自动配置的映射。
再来看看数据源自动配置的实现注解
怎么找到源码文件?按着command+单击username或password,进去九一切了然