文章目錄
還不瞭解Arthas怎麼安裝啓動的可以翻閱 【Arthas】初始Arthas,安裝使用
本文所有的執行命令,可以把項目 Althas(阿爾薩斯)學習 拉到本地啓動之後,都可以直接執行
Althas中的OGNL使用姿勢
從3.0.5版本增加
Althas支持ognl的格式
參數說明
參數名稱 | 參數說明 |
---|---|
express | 執行的表達式 |
[c:] | 執行表達式的 ClassLoader 的 hashcode,默認值是SystemClassLoader |
[x] | 結果對象的展開層次,默認值1 (如果對象裏面包好對象,這個值大一點可以一起輸出) |
使用示例
1.調用靜態屬性
ognl '@全路徑類目@靜態屬性名'
示例:
ognl '@com.shirc.arthasexample.ognl.OgnlTest@static_str'
2.調用靜態方法
ognl '@全路徑類目@靜態方法名("參數")'
示例一: 簡單入參 返回普通對象
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getPerson("src",18)' -X 1
示例二:簡單入參 返回對象中包含對象和List
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getPerson("src",18,2)' -x 1
-x 1 中的x是小寫; 上面可以看到 child對象和childs列表都沒有打印出來
試試 -x 2
和 -x 3
-x 2 的時候對象屬性有展開,但是列表沒有, -x 3 才把列表展開了
示例三: 方法A的返回值當做方法B的入參
ognl '#[email protected]@getPerson("src",18), #[email protected]@setPerson(#value1) ,{#value1,#value2}' -x 2
示例四:執行多行表達式,賦值給臨時變量,返回一個List:
$ ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'
@ArrayList[
@String[/opt/java/8.0.181-zulu/jre],
@String[OpenJDK Runtime Environment],
]
示例五: 方法入參是簡單類型列表
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getChilds({"jinjidelaomanong","jjdlmn"})' -x 2
示例六: 方法入參是一個複雜對象
先用構造函數構造一個對象
ognl 'new com.shirc.arthasexample.ognl.Shirc("jjdlmn",true)'
然後把這個對象當做入參傳入;所以最終可以這麼寫
ognl '#obj=new com.shirc.arthasexample.ognl.Shirc("jjdlmn",true),@com.shirc.arthasexample.ognl.OgnlTest@inputObj(#obj)' -x 2
示例七: 方法入參是一個Map對象
先構造一個Map對象可以這樣
ognl '#{ "foo" : "foo value", "bar" : "bar value" }'
然後把這個對象賦值給一個變量; 最後把這個變量當做入參傳入;
然後把這個對象當做入參傳入;所以最終可以這麼寫
ognl '#inputmap=#{ "foo" : "foo value", "bar" : "bar value" }, @com.shirc.arthasexample.ognl.OgnlTest@getMap(#inputmap)' -x 2
3.讀取不同類型的值
讀取 複雜對象、List、Map等等值的方式
示例一:訪問複雜對象屬性
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getPerson("src",18).name' -x 4
示例二、訪問List或者數組類型
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getChilds({"jinjidelaomanong","jjdlmn"})[0]' -x 2
示例三: 訪問Map對象
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getMap()["shirc"]' -x 2
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getMap()["shirc"].sex' -x 2
shirc: 是map的key; 記得要用雙引號"" 引起來
Ognl 常用用法說明
#
變量引用 #this
當前對象
OGNL的變量方案很簡單, 你可以用變量來保存中間結果, 並在後面的代碼中再次訪問它, 也可以用變量來使整個表達式更加簡單易懂. OGNL中的所有變量, 對整個表達式都是全局可見的. 引用變量的方法是在變量名之前加上
# 號
OGNL在計算表達式的過程中, 隨時會將當前對象保存在"this"
變量中, 這個變量也可以象其他任何變量一樣引用,用#this
表示當前對象
例如:
ognl '@com.shirc.arthasexample.ognl.OgnlTest@getMap()["shirc"].(#this.sex=="boy"?"BoyNB":"GirlNB")' -x 2
調用構造方法
new 全路徑類名()
ognl 'new com.shirc.arthasexample.ognl.Shirc("shirc",true)'
收集特殊使用姿勢
如果有想要實現的姿勢,歡迎留言,博主會把表達式更新到文章中;