![984bd64a824f5e8d91acd163abc48d0d.png](https://i-blog.csdnimg.cn/blog_migrate/86edc74e94cb34d0fbe0466bc48e58f8.jpeg)
往期推荐
如果面试时大家都说真话...
这400道面试题,决定了你去BAT还是TMD
之前我们学习了SpringBoot中的配置文件及外部化配置,了解了SpringBoot对于配置文件的功能支持与增强,本篇我们将要来学习SpringBoot的自动配置原理及手动实现自动配置。
数据库依赖引起的bug
我们很多人在第一次使用SpringBoot的时候,往往对其原理认知不足,或者简单的了解以后就开始入门使用,往往最常见的就是使用SpringBoot添加一个持久化框架的依赖,用来尝试操作数据库,比如:
<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background: rgb(27, 25, 24); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">
1. `<dependency>`
2. `<groupId>org.springframework.boot</groupId>`
3. `<artifactId>spring-boot-starter-jdbc</artifactId>`
4. `</dependency>`
</pre>
接着我们启动SpringBoot,异常出现了,SpringBoot居然启动不了?
![d6fa9f9136b770703fe883653486b983.png](https://i-blog.csdnimg.cn/blog_migrate/18e080d75824c1729682d4e4da61b14a.jpeg)
看报错信息,似乎是我没有配置url导致数据源注入失败,但是我仅仅是引入了一个依赖,什么操作也没有啊?看来SpringBoot帮我们引入的db框架尝试进行自动注入了,我们在Spring中如何配置一个数据源的呢?
<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background: rgb(27, 25, 24); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">
1. `<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"`
2. `destroy-method="close">`
3. `<propertyname="driverClass"value="${driverClass}"/>`
4. `<propertyname="jdbcUrl"value="${jdbcUrl}"/>`
5. `<propertyname="user"value="${user}"/>`
6. `<propertyname="password"value="${password}"/>`
7. `</bean>`
</pre>
那么岂不是代表着,我们仅仅引入一个 spring-boot-starter-jdbc
的依赖,SpringBoot帮我们自动配置了一个dataSource的bean?百度了一下,我们知道,可以在springboot启动类中排除datasource自动配置类来解决此问题,如下:
<pre style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; word-wrap: break-word !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: 0.544px; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background: rgb(27, 25, 24); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); text-align: start; font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">
1. `@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)`
</pre>
这样就可以使得SpringBoot启动的时候不去自动配置数据源,但是SpringBoot是如何实现的自动配置呢?
SpringBoot中配置Bean的几种方式
在思考这个问题之前,我们先来看看,在SpringBoot中我们如果要配置一个Bean,有哪几种方式:
@Service/@Component
在SpringBoot中不建议使用xml配置创建实例Bea