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
0
投稿
猜你喜欢
- 双保险线程,每次启动2个相同的线程,互相检测,避免线程死锁造成影响。两个线程都运行,但只有一个线程执行业务,但都会检测对方的时间戳 如果时间
- 需求:按照起始日期查询出数据库里一段连续日期的住院信息。问题:数据库里的住院信息可能不是完整的,也就是在给出的日期区间里只有若干天的数据,缺
- 在学会了java中io流的使用后,我们对于数组的排序,又多了一种使用方法。大家知道流处理数据的效率是比较理想的,那么在具体操作数组排序上,很
- 本文实例为大家分享了Java实现简单万年历的具体代码,供大家参考,具体内容如下1 要求1、输入年份;2、输入月份;3、输出某年某月的日历。2
- 为什么要用Flyway在日常开发中,我们经常会遇到下面的问题:自己写的SQL忘了在所有环境执行;别人写的SQL我们不能确定是否都在所有环境执
- 什么是异常?最简单的,看一个代码示例:public static void main(String[] args) { &nb
- 本文实例讲述了java取两个字符串的最大交集的实现方法,分享给大家供大家参考。具体实现方法如下:package com.itheima.ne
- 简单介绍一下Java中的Excel文件导出功能(基于HttpServletResponse实现下载)首先,引入需要依赖的jar包:<d
- 1. 定义在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。2. 使用的
- 记得在thinkphp框架中,模型名会自动转换为对应下划线的表名,如,UserType 自动转化为user_type,在平时写程序中很多地方
- 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创
- 为了让我提供的通用 Mapper 的 boot-starter 同时兼容 Spring Boot 1.x 和 2.x,增加了这么一个工具类。
- 本文实例讲述了java基于swing实现的五子棋游戏代码。分享给大家供大家参考。主要功能代码如下:import java.awt.*;imp
- 各位亲们可以尝试以下代码:注:这里我就只有一个html标签对来说明问题了,首部之类的东西,自己添加。<html> &n
- 1、判断实体对象是否为空2、判断对象所有属性是否为空3、特别注意,实体类中如果有基本数据类型,会影响判断package com.liuxd.
- 有时候数据库文档需要整理,可是只能手动的复制粘贴,心中一万只草泥马奔腾而过。。。screw简洁好用的数据库表结构文档生成工具。1. 创建项目
- spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>、&
- 注解是 JDK 5.0 引入的一种注释机制。注解可以作用在类型(类、接口、枚举等)、属性、方法、参数等不同位置,具体的 JDK
- hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它为我们的编程提供便利的同时也带来了很多危险.这篇文章我们
- 前言本文主要介绍的是关于CentOS 7配置自定义JDK的方法教程,分享出来供大家参考学习,下面来一起看看详细的介绍:配置教程由于选择的是C