控制 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>
先前的範例使用從 mapping 屬性定義的 Properties
實例和 mappings 屬性定義的路徑中找到的屬性檔案合併而來的 Properties
實例,組態了 KeyNamingStrategy
的實例。在此組態中,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>
請勿將基於介面的 AOP 代理與 bean 類別中 JMX 註解的自動偵測結合使用。基於介面的代理會「隱藏」目標類別,這也會隱藏 JMX 管理的資源註解。因此,在這種情況下,您應該使用目標類別代理(透過在 <aop:config/> 、<tx:annotation-driven/> 等上設定 'proxy-target-class' 標誌)。否則,您的 JMX bean 可能會在啟動時被靜默忽略。 |