springboot starter自定义实现公共模块方式

 更新时间:2024年08月27日 14:13:37   作者:程序猿20  
这篇文章主要介绍了springboot starter自定义实现公共模块方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启动相应的默认配置。

我们将可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配。

比如登录模块,基于aop的日志模块等。

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

以下是定义一个starter的步骤:

1. 建立一个父项目

<?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.9.RELEASE</version>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>springboot-starter-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springboot-starter-config</module>
        <module>springboot-starter-application</module>
    </modules>

    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

2. 新建一个maven module项目作为自定义starter项目

pom.xml如下:

<?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">

    <parent>
        <artifactId>springboot-starter-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-starter-config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

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

</project>

3. 定义属性类

package com.demo;

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

/**
 * 描述:配置信息 实体
 *
 **/
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private String params1;
    private String params2;
    private Integer minLength;


    public String getParams1() {
        return params1;
    }

    public void setParams1(String params1) {
        this.params1 = params1;
    }

    public String getParams2() {
        return params2;
    }

    public void setParams2(String params2) {
        this.params2 = params2;
    }

    public Integer getMinLength() {
        return minLength;
    }

    public void setMinLength(Integer minLength) {
        this.minLength = minLength;
    }
}

4. 定义服务类

package com.demo.service;

import com.demo.DemoProperties;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * author:HUAWEI
 */
public class DemoService {

    @Autowired
    private DemoProperties demoProperties;

    public String params1;
    public String params2;

    public DemoService(String param1, String param2) {
        this.params1 = param1;
        this.params2 = param2;
    }

    public String paramsInfo() {
        return this.params1 + "!  " + params2;
    }

    public boolean isValidLength(String param) {

        int length = param.length();
        Integer minLength = demoProperties.getMinLength();

        if (length < minLength.intValue()) {
            return false;
        } else {
            return true;
        }

    }
}

5. 定义配置类

package com.demo;

import com.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述:配置类
 *
 **/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
        prefix = "demo",
        name = "enable",
        havingValue = "true"
)
public class DemoStarterAutoConfig {
    @Autowired
    private DemoProperties demoProperties;

    @Bean(name = "demo")
    public DemoService demoService(){
        return new DemoService(demoProperties.getParams1(), demoProperties.getParams2());
    }
}

6. 重要的一步

resource目录下添加META-INF目录,在其下面建立文件spring.factories,内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.DemoStarterAutoConfig

经过以上步骤,starter就完成了。

以下是建立测试项目的步骤。

7. 建立maven module测试项目

<?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">
    <parent>
        <artifactId>springboot-starter-demo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-starter-application</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

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

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springboot-starter-config</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

8. 添加配置文件application.yml

server:
  port: 8010


demo:
  enable: true
  params1: 参数1
  params2: 参数2
  minLength: 4

9. 添加测试controller

package com.demo.starter.controller;

import com.demo.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class AppController {

    @Resource(name = "demo")
    private DemoService demoService;

    @GetMapping("/test")
    public String test(){

        boolean valid = demoService.isValidLength("test");
        if(valid)
            return demoService.paramsInfo();
        else
            return "无效数据";
    }
}

10. 打开浏览器,执行测试

自此完成了starter的基本测试。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java使用fastjson对String、JSONObject、JSONArray相互转换

    Java使用fastjson对String、JSONObject、JSONArray相互转换

    这篇文章主要介绍了Java使用fastjson对String、JSONObject、JSONArray相互转换,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • springboot如何重定向携带数据 RedirectAttributes

    springboot如何重定向携带数据 RedirectAttributes

    这篇文章主要介绍了springboot如何重定向携带数据 RedirectAttributes,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • JAVA中的deflate压缩实现方法

    JAVA中的deflate压缩实现方法

    下面小编就为大家带来一篇JAVA中的deflate压缩实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Spring Bean常用依赖注入方式详解

    Spring Bean常用依赖注入方式详解

    这篇文章主要介绍了Spring Bean常用三种依赖注入方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringBoot整合SpringDataJPA的示例

    SpringBoot整合SpringDataJPA的示例

    本文主要介绍了SpringBoot整合SpringDataJPA的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • IntelliJ IDEA 2020.3.3现已发布!新增“受信任项目”功能

    IntelliJ IDEA 2020.3.3现已发布!新增“受信任项目”功能

    这篇文章主要介绍了IntelliJ IDEA 2020.3.3现已发布!新增“受信任项目”功能,本文给大家分享了idea2020.3.3激活码的详细破解教程,每种方法都很好用,使用idea2020.3以下所有版本,需要的朋友可以参考下
    2021-03-03
  • Mybatis-Plus开发提速器mybatis-plus-generator-ui详解

    Mybatis-Plus开发提速器mybatis-plus-generator-ui详解

    这篇文章主要介绍了Mybatis-Plus开发提速器mybatis-plus-generator-ui,本文简要介绍一款基于Mybatis-Plus的代码自助生成器,文章通过实例集成的方式来详细讲解mybatis-plus-generator-ui,从相关概念到实际集成案例,以及具体的扩展开发介绍,需要的朋友可以参考下
    2022-11-11
  • Spring中事件发布机制及流程详解

    Spring中事件发布机制及流程详解

    这篇文章主要介绍了Spring中事件发布机制及流程详解,在分析源码的过程中,也是大量使用了事件机制,在我分析的这篇博客中,有不少地方都运用了事件发布机制,所以本文的目的是从SpringBoot中学习到事件的发布流程,需要的朋友可以参考下
    2023-11-11
  • Java中的ArrayList底层源码分析

    Java中的ArrayList底层源码分析

    这篇文章主要介绍了Java中的ArrayList底层源码分析,通过下标读取元素的速度很快,这是因为ArrayList底层基于数组实现,可以根据下标快速的找到内存地址,接着读取内存地址中存放的数据,需要的朋友可以参考下
    2023-12-12
  • Java中对象序列化与反序列化详解

    Java中对象序列化与反序列化详解

    这篇文章主要介绍了Java中对象序列化与反序列化,较为详细的分析了java中对象序列化的概念、原理、实现方法及相关注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09

最新评论