利用arthas 调用任意bean中的方法

从spring context里获取任意bean

步骤1 获取所有加载com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory的classLoaderHash

sc -d com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory



class-info com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory

code-source /home/software/tomcat_8080/webapps/market-base-sensor-impl-1.4.0-SNAPSHOT/WEB-INF/lib/dubbo-2.6.0.jar

name com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory

isInterface false

isAnnotation false

isEnum false

isAnonymousClass false

isArray false

isLocalClass false

isMemberClass false

isPrimitive false

isSynthetic false

simple-name SpringExtensionFactory

modifier public

annotation

interfaces com.alibaba.dubbo.common.extension.ExtensionFactory

super-class +-java.lang.Object

class-loader +-ParallelWebappClassLoader

context: market-base-sensor-impl-1.4.0-SNAPSHOT

delegate: false

----------> Parent Classloader:

java.net.URLClassLoader@6f1fba17

+-java.net.URLClassLoader@6f1fba17

+-sun.misc.Launcher$AppClassLoader@18b4aac2

+-sun.misc.Launcher$ExtClassLoader@41fecb8b

classLoaderHash 2e0d01ab



步骤2

使用ognl调用bean

//@Autowired

//private RedisTemplate redisTemplate;

[arthas@30418]$ ognl -c 2e0d01ab '#context=@com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory@contexts.iterator.next, #context.getBean("interruptMonitorServiceImpl").redisTemplate.opsForValue().get("stylelogexportdatadelete")'

@Integer[1]

//BaseDTO add(BaseQUERY interruptMonitor);

[arthas@30418]$ ognl -c 2e0d01ab '#context=@com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory@contexts.iterator.next, #context.getBean("interruptMonitorServiceImpl").add(null)'

@BaseDTO[

serialVersionUID=@Long[5602191025869544377],

responseCode=@String[E11_ARG_IS_EMPTY],

responseMsg=@String[arg is empty:{placeholder}],

requestId=null,

timestamp=@String[1587347158026],

dataKey=null,

data=null,

]

当方法是复杂对象不是简单的字符串

[arthas@30418]$ ognl -c 2e0d01ab '#context=@com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory@contexts.iterator.next,#data=new com.hanshow.wise.base.sensor.model.sensor.InterruptMonitor(), #query=new com.hanshow.wise.common.jo.BaseQUERY(),#query.setData(#data),#query.setRequestId("1"), #data.setMerchantId("2156001"),#data.setStoreCode("HS001"),#context.getBean("interruptMonitorServiceImpl").add(#query)'
实际参数{
    "data":{
        "storeCode":"HS001"
    },
    "merchantId":"2156001",
    "requestId":"1"
}
@BaseDTO[

serialVersionUID=@Long[5602191025869544377],

responseCode=@String[SUC],

responseMsg=@String[success],

requestId=@String[1],

timestamp=@String[1587370520281],

dataKey=null,

data=null,

]

 

你可能感兴趣的:(Java)