定制你的【Spring Boot Starter】,加速开发效率

摘要: 本文将介绍如何创建一个自定义的 Spring Boot Starter,让您可以封装常用功能和配置,并在多个 Spring Boot 项目中共享和重用。

1. 简介

Spring Boot Starter 是 Spring Boot 框架中的一种特殊的依赖项,它用于快速启动和配置特定功能的应用程序。Starter 实际上是一个 Maven 或 Gradle 项目,它包含了一组预配置的依赖项、默认的配置信息和自动配置类,以帮助开发者快速集成和使用某项功能。

Spring Boot Starter 通常命名为 spring-boot-starter-*,例如 spring-boot-starter-web 用于启动基本的Web应用程序,spring-boot-starter-data-jpa 用于启动使用JPA的数据访问应用程序。

2.背景

在企业微服务开发中,每个微服务模块可能会有不同的异常处理和错误信息输出方式,这样会导致系统的一致性和可维护性下降。为了解决这个问题,您希望创建一个自定义的 Spring Boot Starter,用于统一处理异常和错误信息,并提供一致的错误响应格式。

3.需求

  1. 统一的异常处理:对于常见的异常类型(如 404、500),提供统一的异常处理方式,并返回一致的错误响应。

  2. 统一的错误响应格式:定义一致的错误响应格式,包括错误代码、错误消息、错误详情等信息。

  3. 可定制化:允许开发人员根据实际情况定制异常处理和错误响应的方式,以满足不同的业务需求。

4.实现

4.1.创建SpringBoot项目

创建完成后项目结构

4.2.添加相关依赖

这里需要注意的是,在build.gradle文件中,我们在这里引入了  id 'java-library'

plugins {
    id 'java'
    // 添加 maven-publish 插件 主要是为了发布项目到 Maven 仓库
    id 'maven-publish'
    // 应用 Java Library 插件,提供 Java 项目的基本构建功能
    id 'java-library'

}

group = 'org.sys_my_start'
version = '1.0-SNAPSHOT'
ext {
    springfoxVersion = '3.2.2'
}

// 配置发布任务
publishing {
    // 定义发布内容
    publications {
        // 定义一个 MavenPublication 类型的发布
        mavenJava(MavenPublication) {
            // 发布内容来源于 Java 组件
            from components.java
            // 定义 Maven 坐标信息
            groupId 'org.sys_my_start' // Maven 项目的组织或公司标识
            artifactId 'com_sys_my_start' // 项目在 Maven 仓库中的唯一标识
            version '1.0-SNAPSHOT' // 项目的版本号,表示开发中的版本
        }
    }
}

repositories {
    mavenCentral()
}

/*  api: 这个依赖声明范围表示该依赖项不仅仅对当前项目的编译和运行时可见,而且对于其他依赖于当前项目的项目也是可见的。
    换句话说,当一个项目依赖于当前项目时,它也将自动获得该依赖项。(也就是最外层的可以访问最内层所引入的依赖)
    上面引入这个 也是为了使用api 因为 gradle 7.0 以后默认不支持 compile语法了废弃了,所以我们这里需要使用api
    id 'java-library'
*/
dependencies {
    api "org.springframework.boot:spring-boot-starter-web:$springfoxVersion"
}

test {
    useJUnitPlatform()
}

4.3.编写Starter 主要逻辑

4.3.1.编写异常处理类

package com.sys.my.start.exception;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    private final ExceptionHandlerProperties properties;

    @Autowired
    public GlobalExceptionHandler(ExceptionHandlerProperties properties) {
        this.properties = properties;
    }

    /**
     * 处理自定义异常
     * @param ex 自定义异常
     * @param request 请求
     * @return ResponseEntity 返回异常信息
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ErrorResponse> handleAnyException(Exception ex, WebRequest request) {
        if (properties.isEnabled()) {
            // 处理自定义异常, 返回异常信息
            ErrorResponse errorResponse = new ErrorResponse("500", ex.getMessage(), request.getDescription(false));
            return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
        } else {
            // 未启用全局异常处理 返回500
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

4.3.2.编写错误响应类

package com.sys.my.start.exception;

/**
 * 定义统一错误相应格式
 */
public class ErrorResponse {
    /**
     * 错误码
     */
    private String errorCode;

    /**
     * 错误信息
     */
    private String errorMessage;

    /**
     * 错误详情
     */
    private String errorDetails;

    public ErrorResponse(String number, String message, String description) {
        this.errorCode = number;
        this.errorMessage = message;
        this.errorDetails = description;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public String getErrorDetails() {
        return errorDetails;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void setErrorDetails(String errorDetails) {
        this.errorDetails = errorDetails;
    }
}

4.3.3.编写扫描配置文件类

package com.sys.my.start.exception;

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

/**
 * 扫描 exception-handler:
 *      enabled: true
 * 为true时,启用全局异常处理
 */
@Component
@ConfigurationProperties(prefix = "exception-handler")
public class ExceptionHandlerProperties {
    private boolean enabled;

    public boolean isEnabled() {
        return enabled;
    }

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

4.3.4.编写自动配置类

package com.sys.my.start.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;

import java.lang.invoke.MethodHandles;

/**
 * 自动配置类
 * 通过@Import导入相关文件
 * EnableAspectJAutoProxy开启AOP代理自动配置
 * @author 13723
 * @version 1.0
 * 2024/2/16 19:16
 */
@Configuration
@EnableAspectJAutoProxy(
		exposeProxy = true
)
@Import({ExceptionHandlerProperties.class})
public class ErrorHandlerAutoProxy {
	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
}

4.3.5.编写spring.factories

# 通过SpringBoot的自动注入功能,扫描spring.factories,实现自动注入。
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.sys.my.start.exception.ErrorHandlerAutoProxy

4.3.6.发布到本地gradle

5.测试

5.1.测试模块引入starter

注意 这里为了保证测试的service模块能够使用到 starter引入springboot依赖,也需要使用api进行调用

// sys_my_service 子项目中的 build.gradle
plugins {
    id 'java-library'
}
dependencies {
    // https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2
    /* json转换 */
    implementation 'com.alibaba.fastjson2:fastjson2:2.0.46'
    /* lombok */
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    /* 日志类 */
    api 'ch.qos.logback:logback-classic:1.4.14'
    /* 引入自定义jar包 */
    api  group: 'org.sys_my_start', name: 'com_sys_my_start', version: '1.0-SNAPSHOT'
}
sourceSets.main.resources {
    srcDirs = ['src/main/java']
    include '**/*.xml'
}

5.2.编写测试代码

/**
 * @author 13723
 * @version 1.0
 * 2024/2/16 13:32
 */
@RestController
@RequestMapping("/test")
public class TestController {
	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	@Resource
	private TestService testService;
	@RequestMapping("/hello")
	public String hello(){
		return testService.hello();
	}
}
/**
 * @author 13723
 * @version 1.0
 * 2024/2/16 13:33
 */

@Service
public class TestService {
	private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

	public String hello(){
		throw new RuntimeException("测试统一返回异常!");
	}
}

5.3.测试结果

6.总结

6.1.好处

自定义 Starter 是在 Spring Boot 中实现代码模块化和功能封装的强大工具。它的优势和用途包括:

  • 模块化开发: 自定义 Starter 允许将应用程序的不同功能模块化,使得代码结构更清晰,易于维护和管理。
  • 功能封装: Starter 可以封装特定功能的代码和配置,使得其他开发人员可以轻松地集成和使用这些功能。
  • 提高开发效率: 使用自定义 Starter 可以加速新项目的开发过程,避免重复编写相似的代码,提高开发效率。
  • 标准化配置: Starter 可以定义标准化的配置方式,使得不同项目之间的配置更一致,降低了配置错误的可能性。

通过自定义 Starter,开发团队可以实现代码的复用和标准化,从而提高了代码的复用性和可维护性,降低了开发和维护成本。

6.2.结语

本文介绍了如何创建自定义的 Spring Boot Starter,并详细解释了其优势、用途以及如何提高代码复用性和可维护性。通过自定义 Starter,开发人员可以更轻松地构建模块化的应用程序,并在团队内部实现功能的共享和复用。

鼓励读者在实际项目中尝试创建自己的自定义 Starter,将通用的功能模块化,并通过开源社区与他人分享,促进技术的共享和交流。这将有助于提高团队的开发效率,加速项目的上线和迭代过程,推动软件开发行业的发展。

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot StarterSpring Boot 应用程序的预配置模块,可以帮助我们快速集成常用框架。MyBatis 是一个持久层框架,用于映射 Java 对象到数据库表。 如果要在 Spring Boot 应用程序中使用 MyBatis,可以创建自定义的 Spring Boot Starter 来简化配置。 步骤如下: 1. 创建一个 maven 项目,并在 pom 文件中添加依赖 spring-boot-starter、mybatis-spring-boot-starter。 2. 创建一个配置类,来配置 MyBatis。 3. 创建一个自动配置类,用于自动配置 MyBatis。 4. 创建一个 starter 类,用于向 Spring Boot 提供自动配置。 5. 在 pom 文件中添加相关信息,用于发布到 maven 仓库。 6. 发布到 maven 仓库,并在其他项目中使用。 如果你想要详细了解,可以参考官网上关于 Spring Boot Starter 的文档。 ### 回答2: 要自定义 Spring Boot Starter 操作 MyBatis 数据库,可以按照以下步骤进行操作: 1. 创建一个 Maven 项目,并指定父项目为 Spring Boot Starter Parent。 2. 在项目的 pom.xml 文件中添加必要的依赖,包括 Spring Boot Starter、MyBatis 和相应的数据库驱动程序。 3. 创建一个自定义的配置类,用于配置 MyBatis 的数据源、事务管理器等。 4. 在配置类中使用 @Configuration 注解标注该类为配置类,并使用 @EnableConfigurationProperties 注解引入配置属性。 5. 创建一个自定义的 Starter 类,用于自动配置 MyBatis 相关的组件。 6. 在 Starter 类中使用 @Configuration 注解标注该类为配置类,并使用 @EnableAutoConfiguration 注解启用自动配置。 7. 在 Starter 类中使用 @ConditionalOnClass 注解,指定条件,在类路径下存在 MyBatis 相关的类时才进行自动配置。 8. 在 Starter 类中使用 @Import 注解,导入配置类,将自定义的配置应用到 Spring Boot 项目中。 9. 编写自定义的配置文件,用于配置 MyBatis 的相关属性,例如数据库连接信息、Mapper 扫描路径等。 10. 在项目的 pom.xml 文件中添加 spring.factories 文件,将自定义的 Starter 类注册到 Spring Boot 应用中。 11. 在 Spring Boot 项目中添加对自定义 Starter 的依赖,可通过 Maven 依赖坐标来引入。 12. 配置项目的 application.properties 或 application.yml 文件,指定数据库相关的信息以及其他自定义属性。 经过以上步骤的操作,就可以自定义 Spring Boot Starter 来操作 MyBatis 数据库了。可以通过引入自定义的 Starter 来简化项目的配置,并在应用中直接使用 MyBatis 进行数据库操作,提高开发效率和代码的可维护性。 ### 回答3: 自定义Spring Boot Starter操作Mybatis数据库涉及以下步骤: 1. 创建一个新的Maven项目,并在pom.xml文件中添加Spring Boot和Mybatis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> ``` 2. 创建一个自定义的starter模块,在其中定义配置类和Mybatis相关配置: ```java @Configuration @AutoConfigureAfter(DataSourceAutoConfiguration.class) @EnableConfigurationProperties(MybatisProperties.class) public class MybatisAutoConfiguration { @Autowired private MybatisProperties properties; @Autowired(required = false) private List<Interceptor> interceptors; @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setConfiguration(properties.getConfiguration()); if (interceptors != null) { factory.setPlugins(interceptors.toArray(new Interceptor[0])); } return factory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3. 创建自定义的配置类MybatisProperties,用于定义数据库的相关配置: ```java @ConfigurationProperties(prefix = "spring.mybatis") public class MybatisProperties { private Configuration configuration; // 其他配置... // Getter和Setter方法 } ``` 4. 在resources目录下创建配置文件application.yml,配置数据库相关信息: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: username password: password spring: mybatis: configuration: map-underscore-to-camel-case: true cache-enabled: true ``` 5. 在使用的项目中添加自定义的starter依赖,并在application类上添加@EnableMybatis注解: ```java @SpringBootApplication @EnableMybatis public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上步骤,我们就可以自定义Spring Boot Starter来操作Mybatis数据库了。这样做的好处是,可以将Mybatis的配置和操作封装在starter中,使得项目更加简洁,并且能够方便地重用该starter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值