springboot 引入jdbc驱动_SpringBoot自动配置原理及手动实现自动配置

本文深入探讨了SpringBoot的自动配置原理,从数据库依赖引起的bug出发,解析了SpringBoot如何通过`spring.factories`文件进行自动配置。文章详细介绍了手动实现自动配置的步骤,包括编写Java Config类、创建META-INF/spring.factories文件,并通过实例展示了如何在SpringBoot项目中应用自定义的自动配置。
摘要由CSDN通过智能技术生成

984bd64a824f5e8d91acd163abc48d0d.png

往期推荐

如果面试时大家都说真话...
这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, &quot;Microsoft Yahei&quot; !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

看报错信息,似乎是我没有配置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, &quot;Microsoft Yahei&quot; !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, &quot;Microsoft Yahei&quot; !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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值