SpringBoot使用AOP與注解實(shí)現(xiàn)請(qǐng)求參數(shù)自動(dòng)填充流程詳解
首先定義一個(gè)加在方法上的注解
import java.lang.annotation.*; /** * 開(kāi)啟自動(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è)類過(guò)大。
/** * 需要自動(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; }
編寫對(duì)于不同參數(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(); //先獲取方法注解,如果沒(méi)有方法注解再去尋找參數(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); } }
開(kāi)啟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ù),就說(shuō)明只要參數(shù)名和 常量類中的匹配上,并且存在對(duì)應(yīng)的實(shí)現(xiàn)類,就會(huì)自動(dòng)填充參數(shù)。
帶參數(shù)例如 @AutoParameterFill(includes = {AutoParameterFillConstants.ID,AutoParameterFillConstants.ZHANG_SAN})
這就代表這個(gè)接口只需要填充id和張三兩個(gè)屬性。
到此這篇關(guān)于SpringBoot使用AOP與注解實(shí)現(xiàn)請(qǐng)求參數(shù)自動(dòng)填充流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot參數(shù)自動(dòng)填充內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring?boot常見(jiàn)get?、post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)、參數(shù)自定義注解校驗(yàn)問(wèn)題解析
- SpringBoot請(qǐng)求處理之常用參數(shù)注解介紹與源碼分析
- SpringBoot請(qǐng)求參數(shù)相關(guān)注解說(shuō)明小結(jié)
- SpringBoot常見(jiàn)get/post請(qǐng)求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解
- SpringBoot 攔截器和自定義注解判斷請(qǐng)求是否合法
- SpringBoot http請(qǐng)求注解@RestController原理解析
- spring boot接收請(qǐng)求常用注解示例詳解
相關(guān)文章
JAVA中通過(guò)Redis實(shí)現(xiàn)延時(shí)任務(wù)demo實(shí)例
Redis在2.0版本時(shí)引入了發(fā)布訂閱(pub/sub)功能,在發(fā)布訂閱中有一個(gè)channel(頻道),與消息隊(duì)列中的topic(主題)類似,可以通過(guò)redis的發(fā)布訂閱者模式實(shí)現(xiàn)延時(shí)任務(wù)功能,實(shí)例中會(huì)議室預(yù)約系統(tǒng),用戶預(yù)約管理員審核后生效,如未審批,需要自動(dòng)變超期未處理,使用延時(shí)任務(wù)2024-08-08java動(dòng)態(tài)口令登錄實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了java動(dòng)態(tài)口令登錄實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Java 和 Kotlin Lambda 表達(dá)式示例詳解
Lambda 表達(dá)式是一種簡(jiǎn)潔的函數(shù)表達(dá)方式,可以把函數(shù)作為一個(gè)方法的參數(shù),或者將代碼塊轉(zhuǎn)換為數(shù)據(jù)傳遞,這篇文章主要介紹了Java 和 Kotlin Lambda 表達(dá)式示例詳解,需要的朋友可以參考下2024-06-06Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn)
本文主要介紹了Springboot整合企業(yè)微信機(jī)器人助手推送消息的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Java底層基于鏈表實(shí)現(xiàn)集合和映射--集合Set操作詳解
這篇文章主要介紹了Java底層基于鏈表實(shí)現(xiàn)集合和映射集合Set操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用鏈表實(shí)現(xiàn)集合和映射相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-03-03Java簡(jiǎn)化復(fù)雜系統(tǒng)調(diào)用的門面設(shè)計(jì)模式
Java門面模式是一種結(jié)構(gòu)性設(shè)計(jì)模式,它為復(fù)雜系統(tǒng)提供了一個(gè)簡(jiǎn)單的接口,使得系統(tǒng)的客戶端能夠更加方便地使用系統(tǒng)功能。門面模式通過(guò)封裝復(fù)雜的子系統(tǒng),隱藏系統(tǒng)的實(shí)現(xiàn)細(xì)節(jié),提高了系統(tǒng)的易用性和靈活性2023-04-04