第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使變量可用于連接點而不更改方法簽名并稍后使用它

使變量可用于連接點而不更改方法簽名并稍后使用它

繁星淼淼 2023-06-28 16:27:53
使用 @Around 方面和 Spring boot 時。在 joinPoint 執(zhí)行之前創(chuàng)建變量、在 joinPoint 執(zhí)行期間使其可用以收集其中的數(shù)據(jù)以及在 joinPoint 執(zhí)行之后使用變量中收集的數(shù)據(jù)的最佳方法是什么?假設(shè)是多線程環(huán)境。@Aspect@EnableAspectJAutoProxypublic class SomeConfig {    @Around(value = "@annotation(path.to.my.annotation.here)", argNames = "specificArg")    public void doLogic(ProceedingJoinPoint joinPoint) throws Throwable {        //create local variable X for thread execution here        try{            joinPoint.proceed(); //or joinPoint.proceed(Object[]);        }        finally {        //use local variable X to do some logic        }    }}不想使用自定義注釋更改方法簽名。任何設(shè)計模式或?qū)崿F(xiàn)示例都會有很大幫助。謝謝!
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經(jīng)驗 獲得超6個贊

您可以創(chuàng)建一個保險箱ThreadLocal并設(shè)置所需的變量,然后使用它。


public class VariableContext {


    private static ThreadLocal<String> currentVariable = new ThreadLocal<String>() {

        @Override

        protected String initialValue() {

            return "";

        }

    };


    public static void setCurrentVariable(String tenant) {

        currentVariable.set(tenant);

    }


    public static String getCurrentVariable() {

        return currentVariable.get();

    }


    public static void clear() {

        currentVariable.remove();

    }


}

您可以在這里或其他課程中使用它。


@Aspect

@EnableAspectJAutoProxy

public class SomeConfig {


    @Around(value = "@annotation(path.to.my.annotation.here)", argNames = "specificArg")

    public void doLogic(ProceedingJoinPoint joinPoint) throws Throwable {


        //create local variable X for thread execution here

        try{

            joinPoint.proceed(); //or joinPoint.proceed(Object[]);

        }

        finally {

        //use local variable X to do some logic

            VariableContext.setCurrentVariable("someValue");

            String result = VariableContext.getCurrentVariable();


        }

    }

}


查看完整回答
反對 回復(fù) 2023-06-28
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號