springboot自写插件封包

news2025/2/25 15:53:07

在Spring Boot中自写插件或封包(通常指的是创建自定义的starter)是一种常见的做法,用于将一系列相关的配置和组件打包成一个独立的模块,从而简化依赖管理和配置过程。以下是一个简单的步骤,指导你如何创建一个自定义的Spring Boot Starter:

1. 创建项目

使用你喜欢的构建工具(如Maven或Gradle)创建一个新的项目。

2. 添加依赖

在你的pom.xmlbuild.gradle文件中,添加Spring Boot Starter的依赖。例如,使用Maven的话,你可以这样添加依赖:

<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter</artifactId>  
        <version>${spring-boot.version}</version>  
    </dependency>  
    <!-- 其他依赖 -->  
</dependencies>

xml复制代码

3. 创建自动配置类

创建一个自动配置类,使用@Configuration@EnableConfigurationProperties注解。这个类将负责配置你的插件。

@Configuration  
@EnableConfigurationProperties(MyPluginProperties.class)  
public class MyPluginAutoConfiguration {  
  
    @Autowired  
    private MyPluginProperties properties;  
  
    @Bean  
    public MyPluginService myPluginService() {  
        return new MyPluginServiceImpl(properties);  
    }  
}

4. 创建配置属性类

创建一个配置属性类,使用@ConfigurationProperties注解,定义你的插件需要的配置。

@ConfigurationProperties(prefix = "my-plugin")  
public class MyPluginProperties {  
  
    private String someProperty;  
  
    // getters and setters  
}

5. 创建服务类

创建一个服务类,实现你的插件功能。

public class MyPluginServiceImpl implements MyPluginService {  
  
    private final MyPluginProperties properties;  
  
    public MyPluginServiceImpl(MyPluginProperties properties) {  
        this.properties = properties;  
    }  
  
    // 实现你的插件功能  
}

6. 在src/main/resources/META-INF/spring.factories中注册自动配置类

创建一个spring.factories文件,在src/main/resources/META-INF/目录下  springboot3.x以后用org.springframework.boot.autoconfigure.AutoConfiguration.imports文件代替,

并添加以下内容:

spring.factories   springboot2.x

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\  
com.yourpackage.MyPluginAutoConfiguration

 org.springframework.boot.autoconfigure.AutoConfiguration.imports    springboot3.x

com.yourpackage.MyPluginAutoConfiguration

7. 打包和发布

使用构建工具打包你的项目,并发布到你的Maven仓库或其他仓库。

8. 使用你的插件

在其他Spring Boot项目中,添加你发布的starter作为依赖,然后在application.propertiesapplication.yml中配置你的插件。

my-plugin.some-property=some-value

最后,你可以在项目中使用@Autowired注解注入你的服务类,并开始使用你的插件功能。

这就是创建一个简单的Spring Boot Starter的基本步骤。你可以根据你的需求进行扩展和定制。

部分代码示例

ZxsAutoConfig
package com.zxs.sso.config;


import com.zxs.sso.core.util.JedisUtil;
import com.zxs.sso.properties.ZxsProperties;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(ZxsProperties.class)
@ComponentScan("com.zxs")
public class ZxsAutoConfig implements DisposableBean {

    @Resource
    ZxsProperties zxsProperties;

    @Bean
    public FilterRegistrationBean xxlSsoFilterRegistration() {
        JedisUtil.init(zxsProperties.getRedisAddress());
        FilterRegistrationBean registration = new FilterRegistrationBean();
        
        //逻辑代码
        return registration;
    }

    @Override
    public void destroy() throws Exception {
        
    }
}

 ZxsProperties

package com.zxs.sso.properties;

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

@Component
@ConfigurationProperties(prefix = "zxs")
public class ZxsProperties {

    private String zxsServer;
    private String logout;
    private String excludeds;
    private String redisAddress;
    private Integer expire;
    private Boolean isToken;

    public String getZxsServer() {
        return zxsServer;
    }

    public void setZxsServer(String zxsServer) {
        this.zxsServer = zxsServer;
    }

    public String getLogout() {
        return logout;
    }

    public void setLogout(String logout) {
        this.logout = logout;
    }

    public String getExcludeds() {
        return excludeds;
    }

    public void setExcludeds(String excludeds) {
        this.excludeds = excludeds;
    }

    public String getRedisAddress() {
        return redisAddress;
    }

    public void setRedisAddress(String redisAddress) {
        this.redisAddress = redisAddress;
    }

    public Integer getExpire() {
        return expire;
    }

    public void setExpire(Integer expire) {
        this.expire = expire;
    }

    public Boolean getToken() {
        return isToken;
    }

    public void setToken(Boolean token) {
        isToken = token;
    }
}

 additional-spring-configuration-metadata.json

{
    "properties": [
        {
            "name": "zxs.zxsServer",
            "description": "SSO服务地址.",
            "defaultValue": "http://127.0.0.1:8080/zxs-server",
            "type": "java.lang.String"
        },
        {
            "name": "zxs.login",
            "type": "java.lang.String"
        },
        {
            "name": "zxs.logout",
            "description": "登出路径.",
            "type": "java.lang.String"
        },
        {
            "name": "zxs.excludeds",
            "description": "排除路径.",
            "type": "java.lang.String"
        },
        {
            "name": "zxs.redisAddress",
            "description": "redis地址.",
            "defaultValue": "redis://zxs:zkb123456@127.0.0.1:6379/0",
            "type": "java.lang.String"
        },
        {
            "name": "zxs.isToken",
            "description": "授权类型.",
            "defaultValue": false,
            "type": "java.lang.Boolean"
        },
        {
            "name": "zxs.expire",
            "description": "时长.",
            "defaultValue": 3600,
            "type": "java.lang.Integer"
        }
    ]
}

 org.springframework.boot.autoconfigure.AutoConfiguration.imports

com.zxs.sso.config.ZxsAutoConfig

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1473748.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2024牛客寒假算法基础集训营2

目录 A.Tokitsukaze and Bracelet B.Tokitsukaze and Cats C.Tokitsukaze and Min-Max XOR D.Tokitsukaze and Slash Draw E and F.Tokitsukaze and Eliminate (easy)(hard) G.Tokitsukaze and Power Battle (easy) 暂无 I.Tokitsukaze and Short Path (plus) J.Tokits…

P0故障应对策略之:为什么P0故障难以排查

与大模型探讨P0故障 P0级故障&#xff0c;作为系统中最严重的故障&#xff0c;它们的发生往往带来灾难性的后果和巨大的损失。同时&#xff0c;这类故障的排查与修复也往往复杂而棘手&#xff0c;对整个团队的经验、综合能力、应急处置流程都是巨大的挑战。 排查P0级故障的过程…

react useMemo 用法

1&#xff0c;useCallback 的功能完全可以由 useMemo 所取代&#xff0c;如果你想通过使用 useMemo 返回一个记忆函数也是完全可以的。 usecallback(fn,inputs)is equivalent to useMemo(()> fn, inputs). 区别是:useCallback不会执行第一个参数函数&#xff0c;而是将它返…

linux查看socket信息

netstat netstat 是一个用于显示网络相关信息的命令行工具。它可以显示当前系统的网络连接状态、路由表、接口统计信息等。 下面是一些常见的 netstat 命令选项和用法&#xff1a; 显示所有活动的网络连接&#xff1a; netstat -a 显示所有正在监听的端口&#xff1a; ne…

TiDB之分布式数据库TiDB 操作管理规范【附可下载文档】

一、 目的 为了在软件生命周期内规范数据库相关的设计、开发、运维工作,便于不同团队之间的沟通及协调,制定此文档,以期在相关规范上达成共识和默契,提升相关环节的工作效率及系统的可维护性。同时好的规范,在执行的时候可以培养出好的习惯,好的习惯是软件质量的很好保证…

飞行棋网站源码

最近抖音很火的情侣飞行棋网站源码&#xff0c;这款情侣飞行棋提供了丰富的游戏玩法&#xff0c;可以为情侣、朋友或家人带来欢乐的游戏体验。无论是在家中&#xff0c;还是在聚会、旅行等场合&#xff0c;都可以轻松启动该网站&#xff0c;共同享受游戏的乐趣。 源码获取搜一搜…

【BUG 记录】史诗级 BUG - MYSQL 删库删表却没有备份如何恢复数据

【BUG 记录】史诗级 BUG - MYSQL 删库删表却没有备份如何恢复数据 1. 问题描述2. 解决方案&#xff08;binlog&#xff09;2.1 构造测试环境2.2 查看 MySQL 环境是否开启 binlog2.3 查看所有的 binlog 日志记录2.4 查看当前正在使用的是哪一个 binlog 文件2.5 查看此时的 binlo…

Redis 发布订阅详解

Redis 发布订阅 Redis 发布订阅 (pub/sub) 是一种消息通信模式&#xff1a;发送者 (pub) 发送消息&#xff0c;订阅者 (sub) 接收消息。Redis 客户端可以订阅任意数量的频道。 Redis 有两种发布订阅模式 基于频道&#xff08;Channel&#xff09;的发布订阅基于模式&#xff…

腾讯云4核8G服务器有用过的吗?性能咋样?

腾讯云4核8G服务器支持多少人在线访问&#xff1f;支持25人同时访问。实际上程序效率不同支持人数在线人数不同&#xff0c;公网带宽也是影响4核8G服务器并发数的一大因素&#xff0c;假设公网带宽太小&#xff0c;流量直接卡在入口&#xff0c;4核8G配置的CPU内存也会造成计算…

MySQL进阶45讲【26】主库出问题了,从库怎么办?

1 前言 在前面的MySQL进阶45讲【23】MySQL是怎么保证主备一致的&#xff1f;、MySQL进阶45讲【24】MySQL是怎么保证高可用的&#xff1f;和MySQL进阶45讲【25】备库为什么会延迟好几个小时&#xff1f;3篇文章中&#xff0c;介绍了MySQL主备复制的基础结构&#xff0c;但这些都…

Python算法100例-2.5 存钱

完整源代码项目地址&#xff0c;关注博主私信源代码后可获取 1.问题描述2.问题分析3.算法设计4.确定程序框架5.完整的程序 1&#xff0e;问题描述 假设银行整存整取存款不同期限的月利率为&#xff1a; 0.63%&#xff0c;期限为1年&#xff1b; 0.66%&#xff0c;期限为2年…

Java练习(第4天)【总结】反转字符串的9种不同实现方法

一、问题描述 给定一个字符串&#xff0c;输出反转后的字符串。 样例输入1: abc 样例输出1: cba 样例输入2: GeeksforGeeks 样例输出2: skeeGrofskeeG 二、Java实现&#xff08;九个程序&#xff09; 1、实现1 思想&#xff1a; 逐个取 倒序拼 Java代码&#xff1a; i…

测试开发(6)软件测试教程——自动化测试selenium(自动化测试介绍、如何实施、Selenium介绍 、Selenium相关的API)

接上次博客&#xff1a;测试开发&#xff08;5&#xff09;测试分类标准 &#xff1a;按测试对像划分、按是否查看代码划分、按开发阶段划分、按测试实施组织、按是否运行划分、按是否手工划分、按测试地域划分-CSDN博客 目录​​​​​​​ 什么是自动化测试 自动化测试介绍…

【Unity自制手册】Unity—Camera相机跟随的方法大全

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;元宇宙-秩沅 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 秩沅 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a;Uni…

微信小程序蓝牙通信HC08

总结这两天研究的蓝牙串口。人话版资料不多&#xff0c;主要靠翻别人的仓库和文档。 单片机部分&#xff0c;与蓝牙串口通信是通过串口。比我想的要简单&#xff0c;小程序部分&#xff0c;有非常多的服务和特征&#xff0c;而且人话版资料不多。 如果本文有什么问题&#xf…

C# 水排序 微信小游戏

来只 水排序谜题启发式搜索方法_水排序解法小程序-CSDN博客 大神的C语言转换成C# 语言&#xff0c;更多的请看原作者&#xff0c;这里直接贴C#代码 using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ConsoleApp2 {class Pro…

再见,Visual Basic——曾经风靡一时的编程语言

2020年3月&#xff0c;微软团队宣布了对Visual Basic&#xff08;VB&#xff09;的“终审判决”&#xff1a;不再进行开发或增加新功能。这意味着曾经风光无限的VB正式退出了历史舞台。 VB是微软推出的首款可视化编程软件&#xff0c;自1991年问世以来&#xff0c;便受到了广大…

vue插件——vue-print-nb 实现打印功能

之前写过好多关于打印的功能&#xff0c;通过windowprint组合键来实现打印。 今天遇到一个需求&#xff0c;就是使用vue插件来实现打印功能。 最终效果图如下&#xff1a; 下面介绍解决步骤&#xff1a; 解决步骤1&#xff1a;安装vue-print-nb插件——npm install vue-p…

2024年,“智慧城市”到底是持续拉跨还是稳步向前?

2024年“智慧城市”的发展趋势&#xff0c;总体上来看&#xff0c;是稳步向前的。 随着科技的不断发展&#xff0c;特别是物联网、云计算、大数据、人工智能等技术的日益成熟和普及&#xff0c;智慧城市的建设有了更为坚实的基础。这些技术不仅可以帮助城市实现更高效、智能的…

智慧公厕让社区生活更美好

随着科技的迅猛发展&#xff0c;城市管理、城市服务均使用科技化的手段进行升级改造&#xff0c;社区生活更美好赋予全新的智慧效能&#xff0c;其中智慧公厕也成为了城市环卫设施的新宠。智慧公厕以物联网、互联网、大数据、云计算、5G通信、自动化控制等技术为核心&#xff0…