springboot自写插件封包

本文详细介绍了如何在SpringBoot中创建自定义starter,包括创建项目、添加依赖、配置自动配置和配置属性类,以及在META-INF中注册和使用自定义插件的过程。
摘要由CSDN通过智能技术生成

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

斗码士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值