好像你在這裏有一些矛盾的要求。
- 您想更改應用程序中的最小代碼。
- 您希望成爲所有屬性讀取和/或寫入的原因。
- 你可能會也可能不會在談論
System.getProperty(...)
。如果沒有,那麼我想你只是在談論各種物體的領域。
有(至少)兩種方法來做到這一點。如果不知道現在如何從源代碼導出mbeans,我無法根據您的具體配置調整我的答案。我的回答將反而顯示你如何使用我的SimpleJMX package來揭露你的屬性。
如果你在談論System.getProperty(...)
那麼你可以寫一個JMX MBean的,可以查詢任何財產和返回值:
@JmxOperation(description = "Query for property")
public String queryForProperty(String name) {
return System.getProperty(name);
}
相反,如果你需要的字段從某些對象列表導出,然後您將不得不爲每個要導出的字段添加註釋,否則您將不得不編寫通過委派導出字段的bean。我看不到任何簡單的方法,並且我知道沒有任何軟件包可以輕鬆地爲您輸出一系列對象,而不需要關於要輸出什麼和如何輸出的信息。
使用SimpleJMX,您可以通過註釋無論是現場還是GET方法導出字段:
@JmxAttributeField(description = "Number of hits in the cache")
private int hitCount;
...
// this can also be specified as @JmxAttributeMethod on the getter/setter methods
@JmxAttributeMethod(description = "Number of misses in the cache")
private int getMissCount() {
return missCount;
}
的@JmxAttributeField
支持isWritable = true
允許值通過JMX進行設置。您也可以使用@JmxAttributeMethod
註釋setter以使其可寫。
如果你不想註釋添加到每個類的,那麼你將不得不寫某種JMX容器類的,通過委託出口值:
public class JmxPublisher {
private Class1 object1;
private Class2 object2;
...
public JmxPublisher(Class1 object1, Class2 object2) {
this.object1 = object1;
this.object2 = object2;
...
}
@JmxAttributeMethod(description = "Number of hits in the cache")
public int getClass1HitCount() {
return object1.getHitCount();
}
@JmxAttributeMethod(description = "Shutdown the background thread")
public void setClass2Shutdown(boolean shutdown) {
return object2.setShutdown(shutdown);
}
...
}
請儘量表達更好的問題。很難閱讀和理解大量未格式化的文字... – JuanZe
「財產」是什麼意思?你說的系統屬性或對象字段或別的東西? – Gray