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 ;
基礎(chǔ)常量類:
/**
* 便于擴展,后續(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ù)名和 常量類中的匹配上,并且存在對應(yīng)的實現(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)文章
JAVA中通過Redis實現(xiàn)延時任務(wù)demo實例
Redis在2.0版本時引入了發(fā)布訂閱(pub/sub)功能,在發(fā)布訂閱中有一個channel(頻道),與消息隊列中的topic(主題)類似,可以通過redis的發(fā)布訂閱者模式實現(xiàn)延時任務(wù)功能,實例中會議室預(yù)約系統(tǒng),用戶預(yù)約管理員審核后生效,如未審批,需要自動變超期未處理,使用延時任務(wù)2024-08-08
Springboot整合企業(yè)微信機器人助手推送消息的實現(xiàn)
本文主要介紹了Springboot整合企業(yè)微信機器人助手推送消息的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
Java底層基于鏈表實現(xiàn)集合和映射--集合Set操作詳解
這篇文章主要介紹了Java底層基于鏈表實現(xiàn)集合和映射集合Set操作,結(jié)合實例形式詳細分析了Java使用鏈表實現(xiàn)集合和映射相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下2020-03-03
Java簡化復(fù)雜系統(tǒng)調(diào)用的門面設(shè)計模式
Java門面模式是一種結(jié)構(gòu)性設(shè)計模式,它為復(fù)雜系統(tǒng)提供了一個簡單的接口,使得系統(tǒng)的客戶端能夠更加方便地使用系統(tǒng)功能。門面模式通過封裝復(fù)雜的子系統(tǒng),隱藏系統(tǒng)的實現(xiàn)細節(jié),提高了系統(tǒng)的易用性和靈活性2023-04-04

