springboot自定义starter启动器

本文详细介绍了如何创建一个自定义的Spring Boot Starter,包括创建模块、配置属性、编写服务类、配置spring.factories文件,以及如何发布和使用。通过这个过程,读者可以了解到Spring Boot Starter的构建步骤和原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。

目录

概 述

第一步、创建 xxx-spring-boot-starter 的spring Initializr模块

在这里插入图片描述

2.修改gav信息即可

###。什么也不选,直接一个空的模块

第二步、删除不需要的内容(启动类、除下面spring-boot-starter的其它依赖,maven编译插件)

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

以下是完整的POM.xml
实际上如果当前starter需要引用其它依赖加入到dependences里面即可,这里只做演示项目:

<?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 https://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.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.huashengshu</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>my-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <!--   保留这个依赖即可,其它依赖都删除     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>
</project>

第三步:
只保留一个一个骨架,对外提供一些自己写的类
创建HelloProperties.java,直接复制下面代码,然后选择包进行粘贴,Idea会自动创建对应类代码设置好包名。

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

@ConfigurationProperties(prefix = "hello") // 对外提供的前缀,相当于其它引入当前starter在properties文件使用hello.属性即可对下面属性进行赋值
public class HelloProperties {

    private String prefix; // 成员属性,意思是前缀名
    private String suffix; // 成员属性,意思是后缀名

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

第三步:写代码,对外提供一些自己写的类

第三步、写代码,对外提供一些自己写的类
创建HelloProperties.java,直接复制下面代码,然后选择包进行粘贴,Idea会自动创建对应类代码设置好包名

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

@ConfigurationProperties(prefix = "hello") // 对外提供的前缀,相当于其它引入当前starter在properties文件使用hello.属性即可对下面属性进行赋值
public class HelloProperties {

    private String prefix; // 成员属性,意思是前缀名
    private String suffix; // 成员属性,意思是后缀名

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

创建方式HelloService.java 直接复制下面的代码,选择包进行黏贴

public class HelloService {

    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name){
        return helloProperties.getPrefix() +" "+name +" "+helloProperties.getSuffix();
    }
}

第四步、在resources资源文件夹下创建一个META-INF文件夹,并创建一个spring.factories文件
内容则是将@Configuration配置类加入,目的是将配置加入到外部的IOC容器中
idea中右键copy–》copy reference,将复制的值填入上面=右边
注意:如果有多个AutoConfiguration则用逗号分开,还有回车小心前面的空格,最好没有其它字符。

例如:
第五步、将该项目发布的maven仓库,或者安装到本地仓库中让其它项目能使用的到

第六步、测试自己定义的启动器使用有效
创建一个springboot项目,勾选web模块即可,然后加入自定义启动器的gav依赖,

properties文件
因为使用了@ConfigurationProperties(prefix = “hello”)注解所以在当前项目的properties文件中使用hello前缀调用即可对成员属性赋值

复制:
hello.prefix=HUASHENGSHU
hello.suffix=Hello World

总结:

相关工具如下:

分析:

小结:

yml配置文件、加载顺序、配置原理~

参考资料和推荐阅读

1.链接: (https://blog.youkuaiyun.com/qq_41813208/article/details/109139783).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

迅捷的软件产品制作专家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值