控制 Bean 的 ObjectName 例項
在幕後,MBeanExporter 將獲取每個註冊 bean 的 ObjectName 例項的職責委託給 ObjectNamingStrategy 的實現。預設情況下,預設實現 KeyNamingStrategy 使用 beans Map 的鍵作為 ObjectName。此外,KeyNamingStrategy 可以將 beans Map 的鍵對映到 Properties 檔案(或多個檔案)中的條目以解析 ObjectName。除了 KeyNamingStrategy,Spring 還提供了兩個額外的 ObjectNamingStrategy 實現:IdentityNamingStrategy(它根據 bean 的 JVM 標識構建 ObjectName)和 MetadataNamingStrategy(它使用原始碼級別元資料獲取 ObjectName)。
從屬性中讀取 ObjectName 例項
您可以配置自己的 KeyNamingStrategy 例項,並將其配置為從 Properties 例項而不是使用 bean 鍵讀取 ObjectName 例項。KeyNamingStrategy 嘗試在 Properties 中查詢與 bean 鍵對應的鍵的條目。如果未找到條目或 Properties 例項為 null,則使用 bean 鍵本身。
以下程式碼顯示了 KeyNamingStrategy 的示例配置
<beans>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="testBean" value-ref="testBean"/>
</map>
</property>
<property name="namingStrategy" ref="namingStrategy"/>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
<bean id="namingStrategy" class="org.springframework.jmx.export.naming.KeyNamingStrategy">
<property name="mappings">
<props>
<prop key="testBean">bean:name=testBean1</prop>
</props>
</property>
<property name="mappingLocations">
<value>names1.properties,names2.properties</value>
</property>
</bean>
</beans>
前面的示例配置了一個 KeyNamingStrategy 例項,其中包含一個 Properties 例項,該例項由對映屬性定義的 Properties 例項和對映屬性定義的路徑中的屬性檔案合併而來。在此配置中,testBean bean 被賦予 ObjectName 為 bean:name=testBean1,因為這是 Properties 例項中具有與 bean 鍵對應的鍵的條目。
如果在 Properties 例項中找不到任何條目,則使用 bean 鍵名稱作為 ObjectName。
使用 MetadataNamingStrategy
MetadataNamingStrategy 使用每個 bean 的 ManagedResource 屬性的 objectName 屬性來建立 ObjectName。以下程式碼顯示了 MetadataNamingStrategy 的配置
<beans>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="testBean" value-ref="testBean"/>
</map>
</property>
<property name="namingStrategy" ref="namingStrategy"/>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
<property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="attributeSource"
class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
</beans>
如果未為 ManagedResource 屬性提供 objectName,則會建立一個具有以下格式的 ObjectName:[完全限定包名]:type=[短類名],name=[bean名]。例如,以下 bean 生成的 ObjectName 將是 com.example:type=MyClass,name=myBean
<bean id="myBean" class="com.example.MyClass"/>
配置基於註解的 MBean 匯出
如果您更喜歡使用基於註解的方法來定義您的管理介面,則可以使用 MBeanExporter 的便捷子類:AnnotationMBeanExporter。定義此子類的例項時,您不再需要 namingStrategy、assembler 和 attributeSource 配置,因為它始終使用標準的基於 Java 註解的元資料(自動檢測也始終啟用)。實際上,除了定義 MBeanExporter bean 之外,@EnableMBeanExport @Configuration 註解或 <context:mbean-export/> 元素還支援更簡單的語法,如以下示例所示
-
Java
-
Kotlin
-
Xml
@Configuration
@EnableMBeanExport
public class JmxConfiguration {
}
@Configuration
@EnableMBeanExport
class JmxConfiguration
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:mbean-export/>
</beans>
如有必要,您可以提供對特定 MBean server 的引用,並且 defaultDomain 屬性(AnnotationMBeanExporter 的一個屬性)接受生成的 MBean ObjectName 域的備用值。它用於代替上一節MetadataNamingStrategy中描述的完全限定包名,如以下示例所示
-
Java
-
Kotlin
-
Xml
@Configuration
@EnableMBeanExport(server="myMBeanServer", defaultDomain="myDomain")
public class CustomJmxConfiguration {
}
@Configuration
@EnableMBeanExport(server="myMBeanServer", defaultDomain="myDomain")
class CustomJmxConfiguration
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:mbean-export server="myMBeanServer" default-domain="myDomain"/>
</beans>
請勿在您的 bean 類中將基於介面的 AOP 代理與 JMX 註解的自動檢測結合使用。基於介面的代理“隱藏”目標類,這也隱藏了 JMX 管理資源註解。因此,在這種情況下,您應該使用目標類代理(透過在 <aop:config/>、<tx:annotation-driven/> 等上設定 'proxy-target-class' 標誌)。否則,您的 JMX bean 可能會在啟動時被靜默忽略。 |