控制 Bean 的 ObjectName 例項

在幕後,MBeanExporter 委託給 ObjectNamingStrategy 的實現來獲取其註冊的每個 Bean 的 ObjectName 例項。預設情況下,預設實現 KeyNamingStrategy 使用 beans Map 的鍵作為 ObjectName。此外,KeyNamingStrategy 可以將 beans Map 的鍵對映到 Properties 檔案(或多個檔案)中的條目以解析 ObjectName。除了 KeyNamingStrategy,Spring 還提供了另外兩個 ObjectNamingStrategy 實現:IdentityNamingStrategy(它根據 Bean 的 JVM 標識構建 ObjectName)和 MetadataNamingStrategy(它使用源級元資料來獲取 ObjectName)。

從 Properties 中讀取 ObjectName 例項

您可以配置自己的 KeyNamingStrategy 例項,並將其配置為從 Properties 例項讀取 ObjectName 例項,而不是使用 Bean 的鍵。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 例項合併了由 mapping 屬性定義的 Properties 例項以及由 mappings 屬性定義的路徑中的屬性檔案。在此配置中,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。定義此子類例項時,您不再需要 namingStrategyassemblerattributeSource 配置,因為它始終使用標準的 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 可能會在啟動時被靜默忽略。