最新国产好看的视频,伊人天堂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)傳過來的參數(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)傳過來的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看起來更干凈一些,我們還是用AOP來實(shí)現(xiàn)吧,順便為AOP更復(fù)雜的應(yīng)用做做鋪墊

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

主配置類如下:

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true) //開啟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)與語句之后的注釋一致

  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開頭的方法,方法帶有兩個(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. 使用修改過的參數(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)單,直接通過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)控的問題

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

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

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

    這篇文章主要給大家介紹了關(guān)于Spring Boot加載配置文件的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(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頁面的實(shí)例

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

    這篇文章主要介紹了springboot返回modelandview頁面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    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í)低的配置,本文就來介紹一下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)論

昌平区| 齐齐哈尔市| 崇信县| 宁晋县| 乌鲁木齐县| 阿坝| 莎车县| 祁连县| 始兴县| 岳西县| 弥勒县| 昌江| 永泰县| 从江县| 南京市| 阳山县| 合肥市| 共和县| 武胜县| 佛山市| 宜黄县| 梧州市| 清水县| 温泉县| 得荣县| 漳平市| 湘潭县| 福鼎市| 基隆市| 西城区| 南开区| 楚雄市| 远安县| 五华县| 始兴县| 安新县| 景宁| 大兴区| 兴义市| 高淳县| 平塘县|