Java教程:学会写Starter-你就懂了SpringBoot自动配置

本文详细介绍了如何从零开始编写一个SpringBoot自动配置的C3P0 Starter,包括Starter的构成、命名规范以及具体实现步骤。通过实践,加深对SpringBoot自动配置原理的理解。
摘要由CSDN通过智能技术生成

一、为什么要写Starter

目前是微服务开发时代,微服务架构,最新编写单元,一定是基于SpringBoot技术,即使不是微服务,也基本使用SpringBoot单体应用去掉SSM开发。故在面试中,必问SpringBoot相关技术,其中自动配置是也是必问知识点。当然开发时间越久,开发级别越高,也肯定会在项目中,开发一些自己的组件,所有自己动手写Starter是必备技能。

二、了解Starter的构成

1. starter包含哪些内容

  1. 提供所需要的依赖,解决依赖直接的冲突

  2. 提供自定义配置所需要的类及配置文件

    为了让大家清楚starter的构成,我们来看mybatis-spring-boot-starter包含了哪些内容,如下是mybatis starter的依赖坐标:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

这个mybatis-spring-boot-starter包含了如下内容:

在这里插入图片描述

其中xxx-jdbc、mybatis、mybatis-spring是mybatis关键依赖,其中xxx-autoconfigure就是自动配置相关的jar包,里面包含内容如下:

在这里插入图片描述

其中XXXAutoConfiguration是自动配置类,MybatisProperties是Mybatis的属性配置类(在springboot的yaml文件中,对mybatis写的配置信息就是此类提供编写的规范,此类进行自动读取)。

META-INF/spring.factories提供的自动配置加载的类列表,内容如下:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration,\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

2. starter的命名规范

在自定义starter时,我们还需要遵循命名的规范,让使用者一眼能看出是官方和非官方开发的。

官方的starter写法:spring-boot-starter-*,感兴趣的可以去官方看看常用的starters有哪些?

非官方的starter写法:thirdpartyproject-spring-boot-starter

结论:只要不是spring官方发布的,我们都按非官方的要求编写,都要以-spring-boot-starter结尾。

三、开始动手写Starter

1. 自定义什么starter

​ 在操作数据库的项目中,我们都会选用一些高性能的数据库连接池产品,比如DruidDataSource,官方也提供了整合的starter,比如 “druid-spring-boot-starter”。在这里,我们为了学习自定义starter,准备使用c3p0,因为c3p0的官方没有提供相应的starter,今天带着大家写一个c3p0的starter,通过自定义starter,来学习一下starter的自动配置,要求如下:

  1. 自动配置名字:c3p0-spring-boot-autoconfigure
  2. starter名字: c3p0-spring-boot-starter
  3. 提供使用c3p0的数据库连接属性(让使用者在yaml文件中对连接进行基本配置)
  4. 启动时实现自动装配,实例化一个DataSource,采用c3p0作为连接池

2. 写starter的步骤

  1. 创建一个空项目springboot-starter,把其它模块放在此项目中管理

  2. 创建c3p0-spring-boot-autoconfigure独立模块,提供自动配置需要的相关类**[autoconfigure模块]**

  3. 创建c3p0-spring-boot-starter模块,引入autoconfigure模块 [starter模块]

  4. 创建spring-boot-test-c3p0模块,依赖c3p0-spring-boot-starter的模块 [测试自定义starter]

3. 实现starter的过程

3.1创建空项目springboot-starter
3.2创建 c3p0-spring-boot-autoconfigure

1)导入依赖坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itheima</groupId>
    <artifactId>c3p0-spring-boot-autoconfigure</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring-boot-version>2.4.4</spring-boot-version>
        <c3p0.version>0.9.1.2</c
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值