SpringBoot使用AOP与注解实现请求参数自动填充流程详解
作者:愿做无知一猿 发布时间:2022-08-18 17:30:36
标签:SpringBoot,AOP,参数,自动填充
首先定义一个加在方法上的注解
import java.lang.annotation.*;
/**
* 开启自动参数填充
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
@Inherited
public @interface AutoParameterFill {
/**
* 要填充的字段名,不写的话默认下面类的子类中的字段都要填充
*
* @see AutoParameterFillConstantsBase
*/
String[] includes() default {};
}
编写参数常量类
也就是参数名称,例如 String username 的 username ;
基础常量类:
/**
* 便于扩展,后续反射获取所有子类的常量值
*/
public class AutoParameterFillConstantsBase {
//do nothing
}
扩展的一个常量,拆分是为了将要填充的参数可以进行分类管理,避免一个类过大。
/**
* 需要自动填充参数的字段名称
*/
public class AutoParameterFillConstants extends AutoParameterFillConstantsBase {
public final static String ID = "id";
public final static String ZHANG_SAN = "zhangsan";
public final static String TEST_ENTITY = "testEntity";
}
定义一个接口
@AutoParameterFill
@RequestMapping("/test1")
public Object test1(@RequestParam(required = false) String id,
@RequestParam(required = false) String zhangsan,
@RequestBody TestEntity testEntity) {
return id + "----" + zhangsan + "----" + testEntity;
}
TestEntity:
import lombok.Data;
@Data
public class TestEntity {
private String id;
private String name;
}
编写对于不同参数的处理接口及实现
该类用于根据参数名获得指定实现:
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 处理并找到适配的实现
*/
@Component
public class AutoParameterAdapter implements InitializingBean {
private final Map<String, AutoParameterHandler> handlerMap = new ConcurrentHashMap<>();
@Autowired
private ApplicationContext applicationContext;
@Override
public void afterPropertiesSet() throws Exception {
applicationContext.getBeansOfType(AutoParameterHandler.class).forEach((k, v) -> {
if (StringUtils.isBlank(v.support())) {
return;
}
handlerMap.put(v.support(), v);
}
);
}
public void addParameter(String support, Object[] args, int i) {
handlerMap.get(support).handle(args, i);
}
}
该类为统一接口:
/**
* 处理统一接口
*/
public interface AutoParameterHandler {
/**
* 处理参数赋值
*
*/
void handle(Object[] args, int i);
/**
* 支持的类型
*/
String support();
}
该类为id参数处理实现:
import com.kusch.ares.annos.AutoParameterFillConstants;
import org.springframework.stereotype.Component;
/**
* 处理ID参数
*/
@Component
public class IdAutoParameterFillHandler implements AutoParameterHandler {
@Override
public void handle(Object[] args, int i) {
args[i] = "idididiidididididididid";
}
@Override
public String support() {
return AutoParameterFillConstants.ID;
}
}
该类为zhangsan参数处理实现:
import com.kusch.ares.annos.AutoParameterFillConstants;
import org.springframework.stereotype.Component;
/**
* 处理zhangsan参数
*/
@Component
public class ZhangSanAutoParameterFillHandler implements AutoParameterHandler {
@Override
public void handle(Object[] args, int i) {
args[i] = "0000000000000000";
}
@Override
public String support() {
return AutoParameterFillConstants.ZHANG_SAN;
}
}
该类为TestEntity参数处理实现:
import com.kusch.ares.annos.AutoParameterFillConstants;
import com.kusch.ares.annos.TestEntity;
import org.springframework.stereotype.Component;
/**
* 处理TestEntity参数
*/
@Component
public class TestEntityAutoParameterFillHandler implements AutoParameterHandler {
@Override
public void handle(Object[] args, int i) {
TestEntity testEntity = new TestEntity();
testEntity.setId("TestEntityAutoParameterFillHandler");
testEntity.setName("TestEntityAutoParameterFillHandler");
args[i] = testEntity;
}
@Override
public String support() {
return AutoParameterFillConstants.TEST_ENTITY;
}
}
AOP具体实现
import com.kusch.ares.annos.handler.AutoParameterAdapter;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.reflections.Reflections;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.method.HandlerMethod;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* 处理参数自动填充
*/
@Aspect
@Component
public class AutoParameterFillAop {
@Resource
private AutoParameterAdapter autoParameterAdapter;
@Around(value = "@annotation(com.kusch.ares.annos.AutoParameterFill)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
HandlerMethod handlerMethod = new HandlerMethod(joinPoint.getTarget(), method);
//方法参数
MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
//先获取方法注解,如果没有方法注解再去寻找参数注解
AutoParameterFill annotation = method.getAnnotation(AutoParameterFill.class);
List<String> list = new ArrayList<>();
//获取注解的 includes 属性的值
String[] includes = annotation.includes();
if (ObjectUtils.isEmpty(includes)) {
//获取 AutoParameterFillConstantsBase 所有子类常量类中的所有值
Reflections reflections = new Reflections();
Set<Class<? extends AutoParameterFillConstantsBase>> classes =
reflections.getSubTypesOf(AutoParameterFillConstantsBase.class);
for (Class<? extends AutoParameterFillConstantsBase> item : classes) {
Field[] fields = item.getDeclaredFields();
for (Field field : fields) {
list.add(String.valueOf(field.get(field.getName())));
}
}
} else {
list.addAll(Arrays.asList(includes));
}
//遍历方法参数
for (MethodParameter methodParameter : methodParameters) {
for (String autoParameterFillConstants : list) {
if (autoParameterFillConstants.equals(methodParameter.getParameter().getName())) {
autoParameterAdapter.addParameter(autoParameterFillConstants, args,
methodParameter.getParameterIndex());
}
}
}
return joinPoint.proceed(args);
}
}
开启AOP记得在启动类加上 @EnableAspectJAutoProxy
补充关键jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 反射工具包 -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
使用方式
将你自己的参数名编写到 AutoParameterFillConstants
中,你也可以自己新建常量类,继承AutoParameterFillConstantsBase
即可。
实现AutoParameterHandler
接口,完成其中两个方法的编写。
在要填充的接口上,加上该注解,例如上述controller
@AutoParameterFill
@RequestMapping("/test1")
public Object test1(@RequestParam(required = false) String id,
@RequestParam(required = false) String zhangsan,
@RequestBody TestEntity testEntity) {
return id + "----" + zhangsan + "----" + testEntity;
}
不带参数,就说明只要参数名和 常量类中的匹配上,并且存在对应的实现类,就会自动填充参数。
带参数例如 @AutoParameterFill(includes = {AutoParameterFillConstants.ID,AutoParameterFillConstants.ZHANG_SAN})
这就代表这个接口只需要填充id和张三两个属性。
来源:https://blog.csdn.net/qq_38397501/article/details/128618252


猜你喜欢
- 前言说到 ADB 大家应该都不陌生,即 Android Debug Bridge,Android调试桥,身为 Android 开发的我们,熟
- using System; using System.Collections.Generic; using
- 概述按钮组件Button是用户和系统交互的重要组件之一,它按照Material Design风格实现,我们先看下Button的参数列表,通过
- 右击有main方法的类===> Run as===> Run Configurations ===>双击java
- 说明本项目采用 maven 结构,主要演示了 spring mvc + mybatis,controller 获取数据后以json 格式返回
- 问题在本地启动dubbo时,服务注册在本地的zookeeper ,但是注册IP却不是本地的iP。产生问题,导致consumer 找不到pro
- 本文实例讲述了C#远程获取图片文件流的方法。分享给大家供大家参考,具体如下:protected void Page_Load(object
- Google的在Google I/O大会上推出了一款新的开发工具android studio。这是一款基于intellij IDE的开发工具
- 二进制数据一般输入的格式是0x45, 0x3a, 0xc3, 这种数据格式看起来是16进制的字符串,但是实际上在存储的时候每个都对应一个字节
- 一、 springBoot + Mybatis配置完成后,访问数据库遇到的问题首先出现这个问题,肯定是xml文件与mapper接口没有匹配上
- 二分查找又称折半查找,它是一种效率较高的查找方法。折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以
- 托管资源指的是.NET可以自动进行回收的资源,主要是指托管堆上分配的内存资源。托管资源的回收工作是不需要人工干预的,有.NET运行库在合适调
- 1、Alt+*(按钮快捷键)按钮快捷键也为最常用快捷键,其设置也故为简单。在大家给button、label、menuStrip等其他控件的T
- 本文实例讲述了Android使用ContentResolver搜索手机通讯录的方法。分享给大家供大家参考,具体如下:在这个程序中使用Cont
- Spring Boot是什么众所周知 Spring 应用需要进行大量的配置,各种 XML 配置和注解配置让人眼花缭乱,且极容易出错,因此 S
- 记录使用Scroller实现平滑滚动,效果图如下:一、自定义View中实现View的平滑滚动public class ScrollerVie
- 本文实例为大家分享了ScrollView实现滚动效果的具体代码,供大家参考,具体内容如下如果长文本的内容超过一屏幕 则只能显示一屏幕的内容设
- 要完成一个轮播图片,首先想到的应该是使用ViewPager来实现。ViewPager已经有了滑动的功能,我们只要让它自己滚动。再加上下方的小
- 遗传算法是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。它能解决很多问题,
- 引入为什么突然说一下Spring启动原理呢,因为之前面试的时候,回答的那可谓是坑坑洼洼,前前后后,补补贴贴。。。总而言之就是不行,再次看一下