Spel AOP動態賦值

2021-10-11 02:54:46 字數 2799 閱讀 1552

/**

* created by guanjian on 2020/11/30 19:58

*/@documented

@retention

(retentionpolicy.runtime)

@target

(elementtype.method)

public @inte***ce

spel

/**

* created by guanjian on 2020/11/30 20:00

*/@aspect

@component

("spelaspect"

)public

class

spelaspect

system.out.

println

("map= "

+ map)

;//2、獲取到方法形參

string[

] params = discoverer.

getparameternames

(getmethod

(pjp));

system.out.

println

("形參param= "

+ json.

tojsonstring

(params));

//3、獲取到方法實參

object[

] args =

getargs

(pjp)

; system.out.

println

("實參args= "

+ json.

tojsonstring

(args));

//4、構建spel的context

evaluationcontext context =

newstandardevaluationcontext()

;for

(int index =

0; index < params.length; index++

)//5、遍歷map=,把對應的value的形參賦值成實參值

map.

foreach

((k, spel)

->);

/** * 解析string型別

*/string bizid =

getannotation

(pjp)

.bizid()

; system.out.

println

("bizid= "

+ parser.

parseexpression

(bizid)

.getvalue

(context, string.

class))

;//target method execute...

object result = null;

trycatch

(throwable e)

finally

}protected method getmethod

(proceedingjoinpoint pjp)

catch

(nosuchmethodexception e)

return method;

}protected object[

]getargs

(proceedingjoinpoint pjp)

protected

static spel getannotation

(proceedingjoinpoint pjp)

catch

(exception e)

return

(spel) annotation;

}}

/**

* created by guanjian on 2020/11/30 20:13

*/@component

("spelservice"

)public

class

spelservice",

bizid =

"#req.bizid"

, operation = operation.start

)public object test

(user req)

}

@runwith

(springjunit4classrunner.

class

)@contextconfiguration

("/spring-config.xml"

)public

class

speltest

}控制台列印輸出

variables=

map=

形參param=

["req"

]實參args=

key=name,spel=#req.name,value=zhangsan

key=age,spel=#req.age,value=

10bizid=

12312212521512

aspect不支援map、list等常用的資料結構做傳參,但實際業務場景中需要使用的話需要做些變通。基於spel提供的強大解析功能能夠非常方便的拆解map、list等,通過parameternamediscoverer和aop進行形參、實參解析和匹配,根據配置的spel表示式進行解析,最終達到動態賦值的目的。

router link動態賦值

a router路由配置 export default newrouter linkactiveclass ac b.template內部 v for data in tuijialist.playlists to span src data.coverimgurl i p router link ...

checkbox動態賦值

審批 交款人 號牌號碼 號牌種類 收費專案名稱 實收金額 應收金額 處理日期 流水號 333 晉dt1692 小型汽車 超速 200 2007 08 29 15 34 36.0 4108 尉樹彬遼n00034 小型汽車 工本費用 300 2007 06 24 13 14 10.0 3846 尉樹同 ...

router link動態賦值

a router路由配置 export default new router linkactiveclass ac b.template內部 span i p router link li c 在detail頁面上怎麼得到id 值?this.route.query.id 在vue中怎麼動態變換rou...