手写一个starter

什么是Starter?Starter其实就是我们经常在maven中的导入的各种模块,自定义Starter可以快速的满足开发的需求,并且可以重复使用无需粘贴代码,直接从maven仓库中导入即可。那么我们如何自定义一个Starter呢?这里我们以自动记录接口请求时间为例(自定义Starter的知识点只需看主要部分即可):

starter命令规则

这段话的大概意思就是,麻烦大家遵守这个命名规范:

Srping官方命名格式为:spring-boot-starter-{name}

非Spring官方建议命名格式:{name}-spring-boot-starter

项目演示

新建工程

首先新建一个maven 工程,名称定义为demo-spring-boot-starter
在这里插入图片描述

在这里插入图片描述

Pom引入依赖
<?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.llc</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>1.0.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>


    <dependencies>
        <!-- 提供了自动装配功能-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!-- 在编译时会自动收集配置类的条件,写
### 创建自定义 Spring Boot Starter 教程 #### 1. 自定义 Starter 的作用与意义 Spring Boot 中的 Starter 是相对于传统 Spring 的一个重要改进之一。Starter 能够将模块所需的各种依赖进行整合,并根据不同的运行环境自动配置模块内的 Bean,从而简化项目的构建过程[^3]。 #### 2. 构建基础结构 为了创建一个自定义 Starter,通常需要建立三个主要部分: - **Core Module (核心功能模块)**:实现业务逻辑的核心类库。 - **Autoconfigure Module (自动配置模块)**:负责读取外部属性文件并完成相应组件的自动化装配工作。 - **Starter Module (启动器模块)**:用于引入其他两个模块以及第三方依赖包;开发者只需在应用中声明此模块作为依赖即可快速集成特定的功能集。 #### 3. 实现步骤详解 ##### 定义 Maven 工程布局 假设要创建名为 `my-feature` 的 Starter,则可以按照以下方式组织目录结构: ```plaintext my-feature/ ├── my-feature-core/ # 核心功能模块 │ └── src/main/java/com/example/myfeature/core/ └── my-feature-autoconfigure/ # 自动配置模块 ├── src/main/resources/META-INF/spring.factories └── src/main/java/com/example/myfeature/autoconfig/ └── my-feature-spring-boot-starter/ # 启动器模块 └── pom.xml # POM 文件, 添加必要的依赖项 ``` ##### 编写 Core 功能代码 在 `my-feature-core` 下编写具体的业务处理逻辑和服务接口定义。这部分不涉及任何框架级别的初始化操作,只专注于领域模型的设计和算法实现。 ##### 设置 Autoconfiguration 在 `my-feature-autoconditioner/src/main/resources/META-INF/spring.factories` 文件内注册自定义配置类路径: ```properties org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.myfeature.autoconfig.MyFeatureAutoConfigurator ``` 接着,在对应的 Java 类中利用条件注解(如 @ConditionalOnClass 或者 @ConditionalOnProperty)来控制何时加载哪些 Beans : ```java @Configuration(proxyBeanMethods = false) @ConditionalOnClass(MyService.class) public class MyFeatureAutoConfigurator { private final MyProperties properties; public MyFeatureAutoConfigurator(ObjectProvider<MyProperties> provider){ this.properties = provider.getIfAvailable(); } @Bean @ConditionalOnMissingBean public MyService myService(){ return new DefaultMyServiceImpl(this.properties); } } ``` 此处展示了如何基于是否存在指定类型的 bean 来决定是否实例化新的服务对象。 ##### 打包发布 Starter 最后一步是在 `my-feature-spring-boot-starter/pom.xml` 中设置好打包信息和其他所需的依赖关系,比如上面提到过的 core 和 autoconfigure 模块以及其他可能需要用到的开源工具或库。完成后可以通过 mvn clean install 命令将整个工程安装到本地仓库供测试使用。 #### 4. 使用自定义 Starter 一旦完成了上述所有准备工作,就可以像下面这样轻松地把新编写的 starter 加入到任意 Spring Boot 应用程序当中去了: ```xml <dependencies> <!-- ... --> <dependency> <groupId>com.example</groupId> <artifactId>my-feature-spring-boot-starter</artifactId> <version>${project.version}</version> </dependency> <!-- ... --> </dependencies> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱上编程2705

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值