当前位置: 首页 > news >正文

Spring Boot 手写starter!!!

原因:为什么要手写starter???

        原因:简化功能。

实例:以分页为例:写一个starter。

1.首先定义一个PageX注解。

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PageX {
}

2.对这个注解进行增强,PageXAop。

/** Copyright (c) 2020, 2024,  All rights reserved.**/
package cn.scl.pagex.aop;import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;/*** <p>Project: pages - PageXAOP</p>* <p>Powered by scl On 2024-02-18 16:32:09</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@Component
@Aspect
public class PageXAOP {@Pointcut("@annotation(cn.scl.core.annotations.PageX)")public void pointcut(){}@Around("pointcut()")public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("目标方法执行之前");//获取请求RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();//获取参数String pageNum = request.getParameter("pageNum");String pageSize = request.getParameter("pageSize");if(ObjectUtil.isNotEmpty(pageNum) && ObjectUtil.isNotEmpty(pageSize) ){PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));}Object result = pjp.proceed();//执行目标方法System.out.println("目标方法执行之后");return result;}
}

3、定义统一返回格式,ResponseDTO。

/** Copyright (c) 2020, 2024,  All rights reserved.**/
package cn.scl.model.dto;import lombok.Data;/*** <p>Project: pages - ResponseDTO</p>* <p>Powered by scl On 2024-02-18 16:26:38</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@Data
public class ResponseDTO {private int code ; //0为请求成功,非0为请求失败private String message; //返回的信息private Object data; //返回的数据//请求成功调用方法public static ResponseDTO success(Object data){ResponseDTO responseDTO = new ResponseDTO();responseDTO.setCode(0);responseDTO.setData(data);return responseDTO;}//请求失败调用方法public static ResponseDTO error(int code,String message){ResponseDTO responseDTO = new ResponseDTO();responseDTO.setCode(code);responseDTO.setMessage(message);return responseDTO;}
}

4、统一对controller进行处理,MyResponseAdvice。

/** Copyright (c) 2020, 2024,  All rights reserved.**/
package cn.scl.pagex.advice;import cn.hutool.json.JSONUtil;
import cn.scl.core.exception.BizException;
import cn.scl.model.dto.ResponseDTO;
import com.github.pagehelper.Page;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;import java.util.HashMap;
import java.util.Map;/*** <p>Project: pages - MyResponseAdvice</p>* <p>Powered by scl On 2024-02-18 16:48:23</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@RestControllerAdvice
public class MyResponseAdvice implements ResponseBodyAdvice<Object> {@Overridepublic boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {return true;}@Overridepublic Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {if (body instanceof ResponseDTO){return body;}if (aClass == StringHttpMessageConverter.class){return JSONUtil.toJsonStr(body);}ResponseDTO dto = ResponseDTO.success(body);if (body instanceof Page) {Page page = (Page) body;long total = page.getTotal();Map<String, Object> m = new HashMap<>();m.put("total", total);m.put("item", body);dto = ResponseDTO.success(m);}return dto;}
}

5、定义一个属性类,用来是否开启注解,PageXProperties。

/** Copyright (c) 2020, 2024,  All rights reserved.**/
package cn.scl.pagex.properties;import lombok.Data;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;/*** <p>Project: scl-root - PageXProperties</p>* <p>Powered by scl On 2024-02-19 16:59:30</p>* <p>描述:<p>** @author 孙臣龙 [1846080280@qq.com]* @version 1.0* @since 17*/
@Data
@ConfigurationProperties(prefix = "cn.scl.pagex")
@EnableAutoConfiguration
public class PageXProperties {private boolean enable=true;
}

6、将需要装配配的类集中起来,自动装配时只需扫描这一个类即可,PageXAutoConfiguration。

@Configuration
@Import({MyResponseAdvice.class, PageXAOP.class})
@ConditionalOnProperty(prefix = "cn.scl.pagex" ,value = "enable" ,havingValue = "true",matchIfMissing = true)
public class PageXAutoConfiguration { //领头羊
}

7、开启自动装配,在resource目录下新建,META-INF/spring.factories文件。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\cn.scl.pagex.autoconfiguration.PageXAutoConfiguration

8.如何打包发布??

目的:可以把我们书写的jar包发布到maven私有仓库,简称私仓

1. 打开云效

2.点击 非生产库-snapshot

mave release仓库与snapshot仓库区别?

在软件开发中,"Maven release 仓库"和"Maven snapshot 仓库"是两种不同的仓库类型,用于存储Maven构建的软件包。

Maven Release 仓库:

Maven Release 仓库用于存储稳定版本的软件包,这些版本经过测试并且在生产环境中使用。当项目开发到一个稳定的版本时,开发人员可以创建一个发布版本并将其上传到 Maven Release 仓库。这些版本具有固定的版本号,并且不会被覆盖或修改。开发人员和其他用户可以通过指定这些稳定的版本号来下载和使用软件包,以确保他们获取的是稳定且经过验证的代码。

Maven Snapshot 仓库:

Maven Snapshot 仓库用于存储开发中的版本或快照版本的软件包。这些版本在开发过程中可能经常变化,并且不稳定。开发人员可以将项目的最新代码以快照版本的形式部署到 Maven Snapshot 仓库中。快照版本具有动态的版本号,通常带有时间戳或其他唯一标识符,以区别于稳定版本。快照版本可以方便地进行测试和共享,但不适合在生产环境中使用,因为它们可能包含未解决的问题或经过充分验证的代码。

这两种仓库的区别在于稳定版本和开发版本之间的区别,以及用途和用例。 Maven Release 仓库用于存储稳定版本,而 Maven Snapshot 仓库则用于存储开发中的快照版本。

3.根据如下步骤,下载setting.xml文件

4.将下载文件放置到 C:\Users\你的用户名\.m2 如下位置,如果你之前有settings.xml文件,请先提前备份.

5. 双击 deploy 就可以发布到私仓,

在私仓就可以看到了对应的jar包,别人就可以直接使用了。

6.如果要想把源码也打包进去,需要maven-source-plugin插件

 <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><phase>package</phase><goals><goal>jar</goal></goals></execution></executions>
</plugin>

XML

再次打包,你会看到私仓多出-source.jar , 别人在使用的时候,可以下载源码查看

目录结构:

相关文章:

  • 第二章、FFmpeg增加RTP协议外部扩展信息解析
  • 蓝桥杯嵌入式第12届真题(完成) STM32G431
  • 内核内存回收关键隐藏变量之page引用计数
  • unity学习(38)——创建(create)角色脚本(panel)--EventSystem
  • 前端不传被删记录的id怎么删除记录,或子表如何删除记录
  • 【深度学习目标检测】十九、基于深度学习的芒果计数分割系统-含数据集、GUI和源码(python,yolov8)
  • 【C语言】详解计算机二级c语言程序题
  • uni-app 经验分享,从入门到离职(四)——页面栈以及页面跳转的 API(开发经验总结)
  • R语言入门笔记2.5
  • AR汽车行业解决方案系列之2-远程汽修
  • 命令行窗口文本复制到 Word 格式保持不变
  • 信息学奥赛一本通1205:汉诺塔问题
  • 接口测试实战--自动化测试流程
  • STM32单片机基本原理与应用(八)
  • hive表中的数据导出 多种方法详细说明
  • Angular 响应式表单 基础例子
  • Angular 响应式表单之下拉框
  • canvas 绘制双线技巧
  • js算法-归并排序(merge_sort)
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • sessionStorage和localStorage
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • 阿里中间件开源组件:Sentinel 0.2.0正式发布
  • 程序员该如何有效的找工作?
  • 对JS继承的一点思考
  • 分享几个不错的工具
  • 如何用vue打造一个移动端音乐播放器
  • gunicorn工作原理
  • Spring Batch JSON 支持
  • # 数论-逆元
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #mysql 8.0 踩坑日记
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (三)Honghu Cloud云架构一定时调度平台
  • .Net 6.0 处理跨域的方式
  • .net MVC中使用angularJs刷新页面数据列表
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .net 使用ajax控件后如何调用前端脚本
  • .NET导入Excel数据
  • .Net接口调试与案例
  • .net连接MySQL的方法
  • .NET企业级应用架构设计系列之技术选型
  • .Net转前端开发-启航篇,如何定制博客园主题
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • @for /l %i in (1,1,10) do md %i 批处理自动建立目录
  • @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
  • []指针
  • [Android开源]EasySharedPreferences:优雅的进行SharedPreferences数据存储操作
  • [Bzoj4722]由乃(线段树好题)(倍增处理模数小快速幂)
  • [Java开发之路](14)反射机制
  • [JS入门到进阶] 前端开发不能写undefined?这是误区!
  • [leetcode top100] 0924 找到数组中消失的数,合并二叉树,比特位计数,汉明距离
  • [LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞