如何自己实现一个Spring Boot Starter

这篇具有很好参考价值的文章主要介绍了如何自己实现一个Spring Boot Starter。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

现在很多开源的组件都会提供对应的 springboot-starter 包给我们去用,要做一个 starter 包并不难。参照Spring内置的实现就好了:
1、在工程里引入 starter 打包相关的依赖。
2、在我们工程内建 spring.factories 文件,编写我们配置类的全限类名。

一、自定义步骤详解

使用AOP实现拦截方法执行和打印日志的功能

1.1、创建Starter项目

名称 zhinian-log-spring-boot-starter , 版本号是 1.0-SNAPSHOT 。且同时引入下方依赖。

1.2、引入pom依赖

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

    <groupId>com.zn.opit.base</groupId>
    <artifactId>zhinian-log-spring-boot-starter</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>
            <version>2.7.13</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.7.13</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

1.3、定义Starter需要的配置类(Properties)

package com.zn.opit.base.config;

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

@ConfigurationProperties(prefix = "zn.log")
public class ZhiNianLogProperties {

    private boolean enabled;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

如果我们需要从 application.yaml 或 application.properties 中拿到一些使用者配置的数据,那么我们就需要定义一个properties类。这个properties类主要作用是将 application.yaml 或 application.properties 中的配置信息映射成实体类,比如我们这里指定 prefix = “zn.log” 这样,我们就能将以zn.log为前缀的配置项拿到了。

1.4、编写Starter项目的业务功能

AOP切面逻辑

package com.zn.opit.base.component;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class WebLogAspect {
    @Pointcut("execution(* *..*Controller.*(..))")
    public void webLog(){}

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        //方法开始前,打印开始日志
        System.out.println("zn-starter --------doBefore");

    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,打印结束日志
        System.out.println("zn-starter --------doAfterReturning");
    }
}

1.5、编写自动配置类

package com.zn.opit.base.config;

import com.zn.opit.base.component.WebLogAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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({ZhiNianLogProperties.class}) // 该注解的作用是为 xxxProperties 开启属性配置功能,并将这个类以组件的形式注入到容器中
@ConditionalOnProperty(prefix = "zn.log", value = "enabled") // 当指定的配置项等于你想要的时候,配置类生效
public class ZnLogAutoConfig {

    @Bean // @Bean:该注解用于将方法的返回值以 Bean 对象的形式添加到容器中
    @ConditionalOnMissingBean // @ConditionalOnMissingBean(xxx.class):该注解表示当容器中没有 xxx 类时,该方法才生效
    public WebLogAspect webLogAspect() {
        return new WebLogAspect();
    }
}

1.6、编写spring.factories文件加载自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zn.opit.base.config.ZnLogAutoConfig

1.7、构建starter制品包

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>

在项目根目录下执行mvn clean install,打包并放到maven仓库中(maven仓库地址和配置有关)
如何自己实现一个Spring Boot Starter,spring

显示build success,表示starter制品包打包完成

二、其它项目引用验证

如何自己实现一个Spring Boot Starter,spring

2.1、pom中引入自定义starter依赖

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

    <groupId>com.zn.opit.base</groupId>
    <artifactId>zhinian-log-starter-test</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>
            <version>2.7.13</version>
        </dependency>

        <dependency>
            <groupId>com.zn.opit.base</groupId>
            <artifactId>zhinian-log-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2.2、写一个Controller接口

(要符合starter里的aop拦截表达式规则,否则拦截不到)

package com.zn.opit.base.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/hello")
@RestController
public class HelloController {

    @GetMapping("/test")
    public String test() {
        return "SUCCESS";
    }
}

2.3、配置文件 application.properties

zn.log.enabled=true
server.port=8077

2.4、验证

启动业务项目,访问接口,验证
如何自己实现一个Spring Boot Starter,spring
如何自己实现一个Spring Boot Starter,spring

至此,一个简单的starter示例就写完了。文章来源地址https://www.toymoban.com/news/detail-814362.html

到了这里,关于如何自己实现一个Spring Boot Starter的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot+jasypt-spring-boot-starter实现配置文件明文加密

    springboot:2.1.4.RELEASE JDK:8 jasypt-spring-boot-starter:3.0.2 Jasypt默认算法为PBEWithMD5AndDES,该算法需要一个加密密钥,可以在应用启动时指定(环境变量)。也可以直接写入配置文件 3.1 application.properties配置文件版 加密后,可删除jasypt.encryptor.password配置;发版时可在命令行中配置 3.2 函数

    2024年02月15日
    浏览(29)
  • Java使用Milo实现OPC UA客户端,封装spring boot starter

    最新版本更新日志查看:https://github.com/kangaroo1122/milo-spring-boot-starter/blob/main/UPDATE.md、https://gitee.com/vampire001/milo-spring-boot-starter/blob/master/UPDATE.md,此处不再更新 由eclipse开源,地址:https://github.com/eclipse/milo,可以基于此开发OPC UA客户端或者服务端。 本文介绍基于milo 封装的sp

    2024年02月09日
    浏览(39)
  • Spring Boot - spring-boot-starter

    spring-boot-starter 当学习Spring Boot时,可以通过一个完整的案例来理解和实践其基本概念和功能。以下是一个简单的Spring Boot Starter完整案例,展示了如何创建一个基本的Web应用程序: 首先,创建一个名为pom.xml的Maven项目文件,添加以下内容:idea或其他直接创建直接跳过!

    2024年02月09日
    浏览(31)
  • shiro-spring-boot-starter针对不同Spring Boot版本

    对于Spring Boot 2.4.10,无法找到shiro-spring-boot-starter的2.7.2版本,这是一个错误的版本号。 shiro-spring-boot-starter针对不同Spring Boot版本,推荐使用的版本如下: Spring Boot 1.x - 使用版本1.4.1 Spring Boot 2.0.x - 使用版本1.5.3 Spring Boot 2.1.x - 使用版本1.6.0 Spring Boot 2.2.x - 使用版本1.7.0 Spring Boot 2.3

    2024年02月13日
    浏览(32)
  • 自定义Spring Boot Starter

    Spring Boot starter 我们知道Spring Boot大大简化了项目初始搭建以及开发过程,而这些都是通过Spring Boot提供的starter来完成的。在实际项目中一些基础模块其本质就是starter,所以我们需要对Spring Boot的starter有一个全面深入的了解,这是我们的必备知识。 starter介绍 spring boot 在配置

    2024年02月10日
    浏览(70)
  • Spring Boot Starter Parent

    在这,您将学习了解 Spring Boot Starter Parent, 它是 Spring Boot 提供的父级 Pom 文件,旨在提供自动版本依赖管理,帮助我们轻松快速地进行 Spring Boot 开发。 通过 Spring Boot Starter Parent, 我们可以进行简单便捷地包依赖管理。在 Spring Boot 每一个发行版中, 均提供了该版本所兼容的依

    2024年02月08日
    浏览(27)
  • Spring Boot中的Actuator是什么?Spring Boot中的Starter依赖是什么?

    在Spring Boot中,Actuator是一种用于监控和管理应用程序的工具。它提供了一些额外的端点和功能,使开发人员能够更好地了解和控制他们的应用程序。 Actuator提供了以下功能: 指标收集:Actuator可以收集并显示有关应用程序的指标,例如内存使用情况、线程数、请求处理时间等

    2024年02月09日
    浏览(34)
  • 自定义 Spring Boot Starter 组件

    自定义 Spring Boot Starter 组件是为了封装和简化特定功能的配置和集成,让用户能够更容易地集成你提供的库或功能。Spring Boot Starter 组件通常包括自动配置、依赖管理和必要的配置。 下面是创建一个简单的 Spring Boot Starter 的基本步骤: 步骤: 创建一个新的 Maven 或 Gradle 项目

    2024年02月05日
    浏览(30)
  • 【Spring Boot 初识丨三】starter

    上一篇讲了如何构建 MAVEN 项目 本篇来讲一讲 starter 依赖项 Spring Boot 初识: 【Spring Boot 初识丨一】入门实战 【Spring Boot 初识丨二】maven 【Spring Boot 初识丨三】starter 【Spring Boot 初识丨四】主应用类   启动器是一组方便的依赖关系描述符,它包含了一系列可以集成到应用里

    2024年02月09日
    浏览(27)
  • Spring Boot Starter介绍和实战

    Spring Boot Starter 是 Spring Boot 提供的一种机制,用于简化和集成应用程序的依赖管理。通过创建自定义的 Starter,可以将一组相关的依赖打包成一个简单的、可重用的模块,使应用程序的配置和依赖管理更加方便。在本文中,我们将深入探讨 Spring Boot Starter 的原理、创建过程,

    2024年01月23日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包