SpringBoot使用AOP與注解實(shí)現(xiàn)請求參數(shù)自動(dòng)填充流程詳解
首先定義一個(gè)加在方法上的注解
import java.lang.annotation.*;
/**
* 開啟自動(dòng)參數(shù)填充
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
@Inherited
public @interface AutoParameterFill {
/**
* 要填充的字段名,不寫的話默認(rèn)下面類的子類中的字段都要填充
*
* @see AutoParameterFillConstantsBase
*/
String[] includes() default {};
}
編寫參數(shù)常量類
也就是參數(shù)名稱,例如 String username 的 username ;
基礎(chǔ)常量類:
/**
* 便于擴(kuò)展,后續(xù)反射獲取所有子類的常量值
*/
public class AutoParameterFillConstantsBase {
//do nothing
}
擴(kuò)展的一個(gè)常量,拆分是為了將要填充的參數(shù)可以進(jìn)行分類管理,避免一個(gè)類過大。
/**
* 需要自動(dòng)填充參數(shù)的字段名稱
*/
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";
}
定義一個(gè)接口
@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;
}
編寫對于不同參數(shù)的處理接口及實(shí)現(xiàn)
該類用于根據(jù)參數(shù)名獲得指定實(shí)現(xiàn):
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;
/**
* 處理并找到適配的實(shí)現(xiàn)
*/
@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);
}
}
該類為統(tǒng)一接口:
/**
* 處理統(tǒng)一接口
*/
public interface AutoParameterHandler {
/**
* 處理參數(shù)賦值
*
*/
void handle(Object[] args, int i);
/**
* 支持的類型
*/
String support();
}
該類為id參數(shù)處理實(shí)現(xiàn):
import com.kusch.ares.annos.AutoParameterFillConstants;
import org.springframework.stereotype.Component;
/**
* 處理ID參數(shù)
*/
@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參數(shù)處理實(shí)現(xiàn):
import com.kusch.ares.annos.AutoParameterFillConstants;
import org.springframework.stereotype.Component;
/**
* 處理zhangsan參數(shù)
*/
@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參數(shù)處理實(shí)現(xiàn):
import com.kusch.ares.annos.AutoParameterFillConstants;
import com.kusch.ares.annos.TestEntity;
import org.springframework.stereotype.Component;
/**
* 處理TestEntity參數(shù)
*/
@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具體實(shí)現(xiàn)
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;
/**
* 處理參數(shù)自動(dòng)填充
*/
@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);
//方法參數(shù)
MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
//先獲取方法注解,如果沒有方法注解再去尋找參數(shù)注解
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));
}
//遍歷方法參數(shù)
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記得在啟動(dòng)類加上 @EnableAspectJAutoProxy
補(bǔ)充關(guān)鍵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>
使用方式
將你自己的參數(shù)名編寫到 AutoParameterFillConstants 中,你也可以自己新建常量類,繼承AutoParameterFillConstantsBase即可。
實(shí)現(xiàn)AutoParameterHandler接口,完成其中兩個(gè)方法的編寫。
在要填充的接口上,加上該注解,例如上述controller
@AutoParameterFill
@RequestMapping("/test1")
public Object test1(@RequestParam(required = false) String id,
@RequestParam(required = false) String zhangsan,
@RequestBody TestEntity testEntity) {
return id + "----" + zhangsan + "----" + testEntity;
}
不帶參數(shù),就說明只要參數(shù)名和 常量類中的匹配上,并且存在對應(yīng)的實(shí)現(xiàn)類,就會自動(dòng)填充參數(shù)。
帶參數(shù)例如 @AutoParameterFill(includes = {AutoParameterFillConstants.ID,AutoParameterFillConstants.ZHANG_SAN}) 這就代表這個(gè)接口只需要填充id和張三兩個(gè)屬性。
到此這篇關(guān)于SpringBoot使用AOP與注解實(shí)現(xiàn)請求參數(shù)自動(dòng)填充流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot參數(shù)自動(dòng)填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring?boot常見get?、post請求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn)問題解析
- SpringBoot請求處理之常用參數(shù)注解介紹與源碼分析
- SpringBoot請求參數(shù)相關(guān)注解說明小結(jié)
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解
- SpringBoot 攔截器和自定義注解判斷請求是否合法
- SpringBoot http請求注解@RestController原理解析
- spring boot接收請求常用注解示例詳解
相關(guān)文章
mybatis錯(cuò)誤之in查詢?<foreach>循環(huán)問題
這篇文章主要介紹了mybatis錯(cuò)誤之in查詢?<foreach>循環(huán)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
springboot 2.x整合mybatis實(shí)現(xiàn)增刪查和批量處理方式
這篇文章主要介紹了springboot 2.x整合mybatis實(shí)現(xiàn)增刪查和批量處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系解析
在我們?nèi)粘5拈_發(fā)中,我們經(jīng)常會用到Filter和Interceptor,這篇文章主要介紹了Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系?,需要的朋友可以參考下2022-10-10
一文探索Apache HttpClient如何設(shè)定超時(shí)時(shí)間
Apache HttpClient是一個(gè)流行的Java庫,用于發(fā)送HTTP請求,這篇文章主要為大家介紹了Apache HttpClient如何設(shè)定超時(shí)時(shí)間,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10
java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例
本文主要介紹了java基于quasar實(shí)現(xiàn)協(xié)程池的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>2022-06-06
SpringBoot項(xiàng)目使用@Scheduled注解實(shí)現(xiàn)定時(shí)任務(wù)的方法
文章介紹了在SpringBoot項(xiàng)目中使用@Scheduled注解實(shí)現(xiàn)定時(shí)任務(wù)的三種方式:基于注解、基于接口和基于注解設(shè)定多線程定時(shí)任務(wù),詳細(xì)講解了@Scheduled注解的使用方法、各個(gè)參數(shù)以及如何配置動(dòng)態(tài)定時(shí)任務(wù)和多線程定時(shí)任務(wù),感興趣的朋友一起看看吧2025-03-03
MyBatis-Plus之邏輯刪除的實(shí)現(xiàn)
這篇文章主要介紹了MyBatis-Plus之邏輯刪除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Spring整合Quartz分布式調(diào)度的示例代碼
本篇文章主要介紹了Spring整合Quartz分布式調(diào)度的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

