SpringBoot使用AOP與注解實現(xiàn)請求參數(shù)自動填充流程詳解
首先定義一個加在方法上的注解
import java.lang.annotation.*;
/**
* 開啟自動參數(shù)填充
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
@Inherited
public @interface AutoParameterFill {
/**
* 要填充的字段名,不寫的話默認下面類的子類中的字段都要填充
*
* @see AutoParameterFillConstantsBase
*/
String[] includes() default {};
}
編寫參數(shù)常量類
也就是參數(shù)名稱,例如 String username 的 username ;
基礎常量類:
/**
* 便于擴展,后續(xù)反射獲取所有子類的常量值
*/
public class AutoParameterFillConstantsBase {
//do nothing
}
擴展的一個常量,拆分是為了將要填充的參數(shù)可以進行分類管理,避免一個類過大。
/**
* 需要自動填充參數(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";
}
定義一個接口
@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ù)的處理接口及實現(xiàn)
該類用于根據(jù)參數(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;
/**
* 處理并找到適配的實現(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ù)處理實現(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ù)處理實現(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ù)處理實現(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具體實現(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ù)自動填充
*/
@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記得在啟動類加上 @EnableAspectJAutoProxy
補充關(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即可。
實現(xiàn)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;
}
不帶參數(shù),就說明只要參數(shù)名和 常量類中的匹配上,并且存在對應的實現(xiàn)類,就會自動填充參數(shù)。
帶參數(shù)例如 @AutoParameterFill(includes = {AutoParameterFillConstants.ID,AutoParameterFillConstants.ZHANG_SAN}) 這就代表這個接口只需要填充id和張三兩個屬性。
到此這篇關(guān)于SpringBoot使用AOP與注解實現(xiàn)請求參數(shù)自動填充流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot參數(shù)自動填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis錯誤之in查詢?<foreach>循環(huán)問題
這篇文章主要介紹了mybatis錯誤之in查詢?<foreach>循環(huán)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
springboot 2.x整合mybatis實現(xiàn)增刪查和批量處理方式
這篇文章主要介紹了springboot 2.x整合mybatis實現(xiàn)增刪查和批量處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教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
java基于quasar實現(xiàn)協(xié)程池的方法示例
本文主要介紹了java基于quasar實現(xiàn)協(xié)程池的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧<BR>2022-06-06
SpringBoot項目使用@Scheduled注解實現(xiàn)定時任務的方法
文章介紹了在SpringBoot項目中使用@Scheduled注解實現(xiàn)定時任務的三種方式:基于注解、基于接口和基于注解設定多線程定時任務,詳細講解了@Scheduled注解的使用方法、各個參數(shù)以及如何配置動態(tài)定時任務和多線程定時任務,感興趣的朋友一起看看吧2025-03-03
Spring整合Quartz分布式調(diào)度的示例代碼
本篇文章主要介紹了Spring整合Quartz分布式調(diào)度的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

