手写一个springboot的starter包

来~ 手写一个spring-boot-starter 前言 我们都知道,Spring Boot最大的特点就是自动装配,简化依赖,可以让我们快速的搭建项目。使用Spring Boot之后,我们要想在项目中使用一些其他框架,只需要引入对应的Starter依赖就可以了。 那么你在实际开发中是否也开发过一些基础功能,这些功能需要在你们的Spring Boot项目中使用呢? 而这些功能可能在其他项目中可能也需要用到,如果我们把相同的功能在另一个项目中重新再写一遍的话肯定不是一个好方法。 我们可以将我们的功能做成一个对应的Starter模块,在项目中要使用.. 阅读详情

Spring Boot Starter 是一组方便的依赖项,它们帮助开发者快速集成 Spring Boot 应用所需的常用功能。通过使用 Starter,开发者可以减少配置和依赖管理的复杂性。

手写一个 Spring Boot Starter 包可以帮助你快速复用某些功能或集成特定的库。下面是创建一个简单的 Spring Boot Starter 的步骤,包括项目结构、代码示例和配置说明。

1. 项目结构

首先,定义一个典型的项目结构:

spring-boot-starter-example
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           ├── AutoConfiguration.java
        │           ├── ExampleService.java
        │           └── ExampleProperties.java
        └── resources
            └── META-INF
                └── spring.factories

2. 创建 pom.xml

创建 Maven 项目的 pom.xml,包含 Spring Boot 相关依赖和插件配置:

<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.example</groupId>
    <artifactId>spring-boot-starter-example</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <spring-boot.version>3.0.0</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. 创建配置类

3.1 ExampleProperties.java

作用:定义自定义的配置属性类,用于映射 application.properties 中的配置项。

  • @ConfigurationProperties:将以 example 为前缀的属性映射到该类中,允许从配置文件中读取值。
  • 提供 getMessage() 和 setMessage() 方法,用于访问和修改配置属性。

用于定义自定义的配置属性:

package com.example;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "example")
public class ExampleProperties {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
3.2 AutoConfiguration.java

作用:实现自动配置的核心逻辑。

  • @AutoConfiguration:标识这是一个自动配置类,Spring Boot 会自动识别它。
  • @EnableConfigurationProperties(ExampleProperties.class):启用 ExampleProperties,使其可以从配置文件中读取属性。
  • exampleService() 方法:创建并返回 ExampleService 的实例,注入 ExampleProperties 中的配置值。

用于自动配置逻辑:

package com.example;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
@EnableConfigurationProperties(ExampleProperties.class)
@AutoConfigureAfter(SomeOtherAutoConfiguration.class) // 可以配置依赖的自动配置
public class AutoConfiguration {

    private final ExampleProperties exampleProperties;

    public AutoConfiguration(ExampleProperties exampleProperties) {
        this.exampleProperties = exampleProperties;
    }

    @Bean
    public ExampleService exampleService() {
        return new ExampleService(exampleProperties.getMessage());
    }
}
3.3 ExampleService.java

作用:实现具体的服务功能。

  • 接收配置值 message,并提供 printMessage() 方法,输出该值。这个类的存在为 Starter 提供了实际功能。

示例服务类,使用自定义的配置属性:

package com.example;

public class ExampleService {
    private final String message;

    public ExampleService(String message) {
        this.message = message;
    }

    public void printMessage() {
        System.out.println("Example message: " + message);
    }
}

4. 创建 spring.factories

作用:用于告知 Spring Boot 自动配置的类。

  • 在 META-INF/spring.factories 文件中,使用 org.springframework.boot.autoconfigure.EnableAutoConfiguration 键将自动配置类注册到 Spring Boot 中。
  • Spring Boot 启动时会查找这个文件,从中读取需要自动配置的类。

在 src/main/resources/META-INF 目录下创建 spring.factories 文件,用于注册自动配置类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.AutoConfiguration

5. 使用 Starter

在另一个 Spring Boot 项目中引入刚刚创建的 Starter:

5.1 在 pom.xml 中添加依赖
<dependency>
    <groupId>com.example</groupId>
    <artifactId>spring-boot-starter-example</artifactId>
    <version>1.0.0</version>
</dependency>
5.2 在 application.properties 中配置
example.message=Hello, Spring Boot Starter!
5.3 使用示例

在 Spring Boot 应用中使用 ExampleService:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartupRunner implements CommandLineRunner {
    private final ExampleService exampleService;

    public StartupRunner(ExampleService exampleService) {
        this.exampleService = exampleService;
    }

    @Override
    public void run(String... args) {
        exampleService.printMessage();
    }
}

带你手写一个SpringBoot starter组件 有接触过starter组件吗?相信大家在接触Spring Boot的项目时,都遇见过像 spring-boot-starter-web等诸如此类的starter组件了吧。用过Spring Boot的会发现它最大的特点就是自动装配,凭借这一特点可以简化依赖,快速搭建项目。那么除了使用之外有没有想了解过如何自定义一个这样的starter组件呢?为什么会想自定义starter组件呢?在实际开发中经常会发现现在开发的功能好像之前项目也开发过呢,那么这些功能能不能封装在一起,让每个项目都可以使用呢? 阅读详情

相关推荐

Spring Boot - 手写一个简单的Starter

上面我们只是完成了功能,但是怎么让别的项目使用这个starter呢?一、在resource目录下新建一个用于保存元数据的目录META-INF二、在META-INF目录下新建一个文件注意:目录名和文件名都是固定写死的!SpringBoot实现自动装配和这块息息相关,体现出SpringBoot约定大于配置的核心理念!三、在中添加自动配置的路径,就是我们配置类的路径。以后引用了这个Starter的项目在启动的时候,其IOC容器会来自动加载指定的配置类。

weixin_54051907的博客 401

手把手教你手写一个最简单的 Spring Boot Starter

{“address”:“拉斯维加斯”,“age”:18,“name”:“Mr.nobody”}%首先新建一个Maven工程(当然也可以其他类型例如 Gradle 工程),在pom.xml文件中引入如下依赖。fastjson 依赖是我们业务用到将 Java 对象转换为 JSON 字符串;spring-boot-configuration-processor 依赖是可选的,加入此依赖主要是打包时,自动生成配置元信息文件,并放入到 jar 中。方便使用者了解到一些配置元信息。4.0.0。

erthrerg的博客 748

简单手写一个Spring boot starter

SpringBoot自定义Starter开发指南 本文介绍了开发SpringBoot自定义Starter的完整流程。Starter能够简化依赖管理,实现即插即用的功能模块。文章以字符串加前缀功能为例,详细说明了实现步骤:1)创建项目并引入核心依赖spring-boot-autoconfigure;2)编写自动配置类(PrefixAutoConfiguration)和属性类(PrefixProperties);3)实现业务逻辑(PrefixService);4)配置spring.factories或AutoC

东皋长歌的专栏 1190

【SpringBoot】手写一个简单的SpringBoot-starter

starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦。 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfiguration。 Starters are a set of convenient dependency descriptors that yo...

fcvtb的博客 3423

莫兰指数stata命令_一文读懂16个sp系列stata空间计量命令及应用

spcompress:Compress Stata-format shapefile压缩数据成stata格式的shapefile2、Looking at data/观测数据2、Looking at data/观测数据grmap:Graph choropleth maps绘制等值区域图spdistance:Calculator for distance between places计算区域之间距离3...

weixin_39719989的博客 821

密码学小白必看:三大顶级会议论文下载与阅读指南(附完整资源链接)

本文为密码学初学者提供了系统获取和阅读三大顶级会议(Crypto、Eurocrypt、Asiacrypt)论文的实战指南。详细介绍了通过IACR官方渠道、ePrint预印本等权威资源免费下载论文的方法,并分享了高效筛选、精读核心章节及构建个人知识体系的具体策略,帮助读者快速切入密码学研究前沿。

weixin_28363123的博客 394

手写一个stater_精通SpringBoot——第五篇:写一个spring-boot-starter包

为了能更好的理解Springboot的自动配置和工作原理,我们今天来手写一个spring-boot-hello-starter。这个过程很简单,代码不多。接下来我们看看怎么开始实践。1. 新建maven工程。这块就不演示了,如果不会可以自行百度...啦啦啦,因为太简单了啊2.新建一个properties类/*** @author Lee* @// TODO 2018/7/25-9:21* @des...

weixin_39977586的博客 154

【两方演化博弈代码复现】:双方演化博弈的原理、概率博弈仿真、相位图、单个参数灵敏度演化

演化博弈论是研究个体在特定环境中如何通过策略选择与其他个体进行互动的学科。这一理论在生物学、经济学和社会科学等多个领域都有广泛应用。本文将深入探讨双方演化博弈的原理及其过程,并解读一段 MATLAB 代码,展示如何模拟这一过程。

当回首往事的时候,不会因虚度年华而悔恨,也不会因碌碌无为而羞愧。 6198

如何手写一个SpringBoot Starter

SpringApplication.run(DemoApplication.class, args);

cable5881的博客 883

那些年对支付宝的研究[整理]

支付宝功能强大.他内部各个功能间的跳转是通过Uri 实现的.而使用window.location.href 即可.跟浏览器一样的协议.很方便 H5启动支付宝: alipays://platformapi/startapp?appId=20000067&url=网址 alipays://platformapi/startapp?appId=20000691&url=网址 ali...

技术专研 8915

Unity 接入HybridCLR的点点滴滴,亲测三平台(PC、Android、WebGL)妥妥的。-问题分享

一入热更深似海啊,没有热更是真恼火啊,干啥啥不方便,动不动就得重新发包;说实在的,也是工作之余研究这个,在原有框架基础上接入这个热更,既要保持原有功能,还要支持热更,实实在在、断断续续搞了这么久,终于是接入并测通了,这一路是坎坎坷坷,下面把走过的弯弯绕绕记录下,希望对后来想接入的小伙伴有帮助吧。

GarFe的博客 1万+

【深入浅出SpringBoot的核心原理】手写Starter

我们通过手写一个starter,实现一个这样的功能:如果存在FastJson包则将对象以json形式输出,否则以对象的toString()输出。另外引入这个starter后我们不希望有任何的配置(自动配置),开箱即用。此文并不探究starter的原理,只希望读者可以初步了解一个starter的开发流程。 步骤1:新建一个空maven工程,并在pom.xml中导入如下依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --&

qq_35529931的博客 1018

国产安路FPGA(二)-TD软件时序约束与硬件调试实战

本文深入探讨了国产安路FPGA开发中TD软件时序约束的实战应用。文章详细解析了SDC文件的核心语法,包括基础时钟、生成时钟及输入输出延迟约束的编写方法,并针对硬件调试中常见的时序问题提供了解决方案。通过一个图像处理流水线的优化案例,阐述了如何结合代码修改与约束设置,实现设计从“能跑”到“跑得稳”的关键提升,是掌握FPGA可靠设计的实用指南。

weixin_29183637的博客 376

SpringBoot之手写starter

SpringBoot手写starter

yuyanhaonan的博客 710

授时中心被攻击?Windows 11 电脑时间不准?手把手教您如何更换国内稳定高速的时间服务器(时间服务器整理)

近期授时中心被攻击,时间同步重要性凸显。Windows 11 默认服务器在国内常遇网络问题。本文教你轻松修改时间服务器,选择更稳定、快速的国内优质服务器,彻底解决电脑时间不准的烦恼,让你的系统时间永远精准如一!🚀

XiaoqiangClub的博客 1082

Debezium报错处理系列之二十八: Expiring 1 record(s) for test-ts-0:120001 ms has passed since batch creation

Debezium报错处理系列之二十八: Expiring 1 record(s) for test-ts-0:120001 ms has passed since batch creation

zhengzaifeidelushang的博客 3290

SpringBoot自动装配原理分析,手写starter组件,java程序员面试宝典答案

} 现实开发中,假如就有这种情况,定义好了一个类,即使加上了注解,也不能保证这个类一定被 Spring 扫描到,这个时候该怎么做呢? 这时候我们可以再定义一个类 MyConfiguration,保证这个类可以被 Spring 扫描到,然后通过加上 @Import 注解来导入 TestImport 类,这时候就可以直接注入 TestImport 了: @Configuration @Import(TestImport.class) public class MyConfiguration { } 所以这里的

m0_64867152的博客 806

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

如何手写一个springBoot的starter

使用SpringBoot,可以快速帮我们搭建一个spring 应用,如果当前项目需要引入第三方的功能实现,我们只需要在当前项目的pom.xml文件中引入第三方的spring-boot-starter包。目前,springboot的社区,提供了很多第三方的spring-boot-starter,但有时候,我们还是需要根据自己项目的情况,编写满足项目需求的spring-boot-starter。在GitHub新建一个仓库,并把weather-service、weather-starter都上传到这个仓库中。

sgjingwen1987的博客 1774

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

SpringBoot-starter原理之手写

目录 一、前言 二、实现思路 2.1 命名规范 2.2 实现步骤 三、手写实现 3.1 新建第三方jar 3.2 新建starter工程 3.3 引入jar包 3.4 编写自动配置类 3.5 编写配置发现文件 3.6 打包发布 四、测试 一、前言 SpringBoot-starter的原理,在之前的文章中已作解析,可点此传送。 今天要来手动实现一个自己的SpringB...

天天向上 8108

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

SpringBoot自动装配原理分析,看完你也能手写一个starter组件

揭秘SpringBoot自动装配机制及手写starter组件

zwx900102的博客 7567

SpringBoot-starter的原理

目录 一、前言 1.1 SpringBoot的优点 1.2 SpringBoot-starter的作用 二、SpringBoot-starter解析 2.1 SpringBoot搭建SSM 2.2 SpringBoot的诸多配置 2.3 SpringBoot-starter自动配置bean 一、前言 1.1 SpringBoot的优点 SpringBoot是新一代流行的Spri...

天天向上 2万+

SpringBoot手写starter 超详细

SpringBoot手写starter 相信大家在公司做项目时经常会使用到公司自己封装的依赖,里面就包括一些starter。可以用于制成通用方法以及框架等方便维护以及后续开发。所以研究了一下如何自己去写。先小试牛刀(在一个maven项目下快速两个创建springboot项目,一个用来写starter,一个用来测试。至于为什么这样建是因为,写完的starter需要打包然后配置下maven私库才能跨项目使用,所以现在同一个工程里做) 首先 创建完的项目就是这样子。stater的命名是有规范的,像官方的就是把自

qq_44772660的博客 7111

SpringBoot中的Starter,我们来手写一个

SpringBoot中的Starter,我们来手写一个 最近一个朋友去面试被问到了springboot中starter,觉得可以来记录与分享一下 我们使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中。Starter 为我们带来了众多的自动化配置,有了这些自动化配置,我们可以不费吹灰之力就能搭建一个生产级开发环境,其实 Starter 也都是 Spring + SpringMVC 中的基础知识点实现的 1.核心 其实 Starter 的核心就是条件注解 @Conditional ,当

qq_34136709的博客 316

如何自定义一个 SpringBoot 的 starter 包

starter 包与自动配置的概念 我们知道 SpringBoot 通过 starter 包引入对应的第三方 jar 包,然后通过自动配置机制来自动创建该第三方 jar 包对应的功能的 bean 对象。如通过引入 spring-boot-starter-data-redis 这个 starter 包并且在 application.properties 属性配置文件配置 Redis 服务器的域名和...

源码之下,了无秘密 2905

SpringBoot简介及手写个starter

SpringBoot简介及手写starter

weixin_45472766的博客 1052

手写一个SpringBoot Starter

手写一个SpringBoot Starter 定义 所谓的 Starter ,就是Springboot中的某一个模块,它把这一一模块的所需要的JAR包打包好给我们并且能报配置文件文件搞好,不需要我们手机配置了,其实就是一个普通的 Maven 项目 。 开整 首先建立一个Maven项目, 创建完成后,添加 Starter 的自动化配置类即可 <dependency> <groupId>org.springframework.boot</groupId>

weixin_44559993的博客 219
上一篇: Spring 中事务的使用方法和原理
下一篇: 自己设计一个注册中心,需要考虑哪些技术点
yymagicer
博客等级 码龄13年 1425粉丝 180原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

yymagicer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值