最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用Spring AOP預(yù)處理Controller的參數(shù)

 更新時(shí)間:2021年08月09日 11:05:46   作者:rw-just-go-forward  
這篇文章主要介紹了如何使用Spring AOP預(yù)處理Controller的參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Spring AOP預(yù)處理Controller的參數(shù)

實(shí)際編程中,可能會(huì)有這樣一種情況,前臺(tái)傳過(guò)來(lái)的參數(shù),我們需要一定的處理才能使用

比如有這樣一個(gè)Controller

@Controller
public class MatchOddsController {
    @Autowired
    private MatchOddsServcie matchOddsService;
    @RequestMapping(value = "/listOdds", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public List<OddsModel> listOdds(@RequestParam Date startDate, @RequestParam Date endDate) {
        return matchOddsService.listOdds(startDate, endDate);
    }
}

前臺(tái)傳過(guò)來(lái)的startDate和endDate是兩個(gè)日期,實(shí)際使用中我們需要將之轉(zhuǎn)換為兩個(gè)日期對(duì)應(yīng)的當(dāng)天11點(diǎn),如果只有這么一個(gè)類的話,我們是可以直接在方法最前面處理就可以了

但是,還有下面兩個(gè)類具有同樣的業(yè)務(wù)邏輯

@Controller
public class MatchProductController {
    @Autowired
    private MatchProductService matchProductService;
    @RequestMapping(value = "/listProduct", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public List<ProductModel> listProduct(@RequestParam Date startDate, @RequestParam Date endDate) {
        return matchProductService.listMatchProduct(startDate, endDate);
    }
}
@Controller
public class MatchController {
    @Autowired
    private MatchService matchService;
    
    @RequestMapping(value = "/listMatch", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    public List<MatchModel> listMatch(@RequestParam Date startDate, @RequestParam Date endDate) {
        return matchService.listMatch(startDate, endDate);
    }
}

當(dāng)然也可以寫兩個(gè)util方法,分別處理startDate和endDate,但是為了讓Controller看起來(lái)更干凈一些,我們還是用AOP來(lái)實(shí)現(xiàn)吧,順便為AOP更復(fù)雜的應(yīng)用做做鋪墊

本應(yīng)用中使用Configuration Class來(lái)進(jìn)行配置,

主配置類如下:

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true) //開(kāi)啟AspectJ代理,并將proxyTargetClass置為true,表示啟用cglib對(duì)Class也進(jìn)行代理
public class Application extends SpringBootServletInitializer {
    ...
}

下面新建一個(gè)Aspect類,代碼如下

@Aspect //1
@Configuration //2
public class SearchDateAspect {
    @Pointcut("execution(* com.ronnie.controller.*.list*(java.util.Date,java.util.Date)) && args(startDate,endDate)") //3
    private void searchDatePointcut(Date startDate, Date endDate) { //4
    }
    @Around(value = "searchDatePointcut(startDate,endDate)", argNames = "startDate,endDate") //5
    public Object dealSearchDate(ProceedingJoinPoint joinpoint, Date startDate, Date endDate) throws Throwable { //6
        Object[] args = joinpoint.getArgs(); //7
        if (args[0] == null) {
            args[0] = Calendars.getTodayEleven();
            args[1] = DateUtils.add(new Date(), 7, TimeUnit.DAYS);//默認(rèn)顯示今天及以后的所有賠率
        } else {
            args[0] = DateUtils.addHours(startDate, 11);
            args[1] = DateUtils.addHours(endDate, 11);
        }
        return joinpoint.proceed(args); //8
    }
}

分別解釋一下上面各個(gè)地方的意思,標(biāo)號(hào)與語(yǔ)句之后的注釋一致

  1. 表示這是一個(gè)切面類
  2. 表示這個(gè)類是一個(gè)配置類,在ApplicationContext啟動(dòng)時(shí)會(huì)加載配置,將這個(gè)類掃描到
  3. 定義一個(gè)切點(diǎn),execution(* com.ronnie.controller.*.list*(java.util.Date,java.util.Date))表示任意返回值,在com.ronnie.controller包下任意類的以list開(kāi)頭的方法,方法帶有兩個(gè)Date類型的參數(shù),args(startDate,endDate)表示需要Spring傳入這兩個(gè)參數(shù)
  4. 定義切點(diǎn)的名稱
  5. 配置環(huán)繞通知
  6. ProceedingJoinPoint會(huì)自動(dòng)傳入,用于處理真實(shí)的調(diào)用
  7. 獲取參數(shù),下面代碼是修改參數(shù)
  8. 使用修改過(guò)的參數(shù)調(diào)用目標(biāo)類

更多可參考

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/

AOP獲取參數(shù)名稱

由于項(xiàng)目中打印日志的需要,研究了一下在aop中,獲取參數(shù)名稱的方法。

1、jdk1,8中比較簡(jiǎn)單,直接通過(guò)joinPoint中的getSignature()方法即可獲取

Signature signature = joinpoint.getSignature();  
MethodSignature methodSignature = (MethodSignature) signature;  
String[] strings = methodSignature.getParameterNames();  
System.out.println(Arrays.toString(strings));  

2.通用方法。比較麻煩

public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{  
          
        String classType = joinPoint.getTarget().getClass().getName();    
        Class<?> clazz = Class.forName(classType);    
        String clazzName = clazz.getName();    
        String methodName = joinPoint.getSignature().getName(); //獲取方法名稱   
        Object[] args = joinPoint.getArgs();//參數(shù)  
          //獲取參數(shù)名稱和值  
        Map<String,Object > nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName,args);   
        System.out.println(nameAndArgs.toString());  
        //為了省事,其他代碼就不寫了,  
        return result = joinPoint.proceed();  
   
}  
private Map<String,Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws NotFoundException {   
        Map<String,Object > map=new HashMap<String,Object>();  
          
        ClassPool pool = ClassPool.getDefault();    
        //ClassClassPath classPath = new ClassClassPath(this.getClass());    
        ClassClassPath classPath = new ClassClassPath(cls);    
        pool.insertClassPath(classPath);    
            
        CtClass cc = pool.get(clazzName);    
        CtMethod cm = cc.getDeclaredMethod(methodName);    
        MethodInfo methodInfo = cm.getMethodInfo();  
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();    
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);    
        if (attr == null) {    
            // exception    
        }    
       // String[] paramNames = new String[cm.getParameterTypes().length];    
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;    
        for (int i = 0; i < cm.getParameterTypes().length; i++){    
            map.put( attr.variableName(i + pos),args[i]);//paramNames即參數(shù)名    
        }    
          
        //Map<>  
        return map;    
    } 

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問(wèn)題

    關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問(wèn)題

    這篇文章主要介紹了關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Spring Boot加載配置文件的完整步驟

    Spring Boot加載配置文件的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot加載配置文件的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • spring?@Conditional的使用與擴(kuò)展源碼分析

    spring?@Conditional的使用與擴(kuò)展源碼分析

    這篇文章主要介紹了spring?@Conditional的使用與擴(kuò)展,這里需要注意如果Condition返回的是false,那么spirng就不會(huì)對(duì)方法或類進(jìn)行解析,具體源碼分析跟隨小編一起看看吧
    2022-03-03
  • Java多線程案例之單例模式懶漢+餓漢+枚舉

    Java多線程案例之單例模式懶漢+餓漢+枚舉

    這篇文章主要介紹了Java多線程案例之單例模式懶漢+餓漢+枚舉,文章著重介紹在多線程的背景下簡(jiǎn)單的實(shí)現(xiàn)單例模式,需要的小伙伴可以參考一下
    2022-06-06
  • springboot返回modelandview頁(yè)面的實(shí)例

    springboot返回modelandview頁(yè)面的實(shí)例

    這篇文章主要介紹了springboot返回modelandview頁(yè)面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • Java中的繼承與接口解讀

    Java中的繼承與接口解讀

    這篇文章主要介紹了Java中的繼承與接口使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • java反射方式創(chuàng)建代碼詳解

    java反射方式創(chuàng)建代碼詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于java反射方式創(chuàng)建代碼詳解內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。
    2021-01-01
  • 淺談SpringBoot中properties、yml、yaml的優(yōu)先級(jí)

    淺談SpringBoot中properties、yml、yaml的優(yōu)先級(jí)

    優(yōu)先級(jí)低的配置會(huì)被先加載,所以優(yōu)先級(jí)高的配置會(huì)覆蓋優(yōu)先級(jí)低的配置,本文就來(lái)介紹一下SpringBoot中properties、yml、yaml的優(yōu)先級(jí),感興趣的可以了解一下
    2023-08-08
  • Java字節(jié)碼ByteBuddy使用及原理解析下

    Java字節(jié)碼ByteBuddy使用及原理解析下

    這篇文章主要為大家介紹了Java字節(jié)碼ByteBuddy使用及原理解析下篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例

    java算法題解??虰M99順時(shí)針旋轉(zhuǎn)矩陣示例

    這篇文章主要為大家介紹了java算法題解牛客BM99順時(shí)針旋轉(zhuǎn)矩陣示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01

最新評(píng)論

枣强县| 望谟县| 定结县| 宜州市| 玉田县| 苏尼特右旗| 紫阳县| 台中市| 高雄市| 遵义县| 乐至县| 通州市| 高邑县| 重庆市| 湾仔区| 大足县| 定南县| 云南省| 泗洪县| 开封县| 民和| 麻栗坡县| 子洲县| 寿阳县| 沈丘县| 谢通门县| 娄底市| 东港市| 钟祥市| 正安县| 白玉县| 衡阳县| 鹤峰县| 萨嘎县| 河池市| 贵州省| 城口县| 荆州市| 金昌市| 白河县| 八宿县|