此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

控制 Bean 的 Management Interface

上一节的示例中, 您对 Bean 的 Management Interface 几乎没有控制权。所有public每个导出的 bean 的属性和方法都公开为 JMX 属性和 作。要对具体内容进行更精细的控制 导出的 bean 的属性和方法实际上作为 JMX 属性公开 和作,Spring JMX 为 控制 bean 的 Management 接口。spring-doc.cadn.net.cn

使用MBeanInfoAssembler应用程序接口

在幕后,MBeanExporterdelegates 的org.springframework.jmx.export.assembler.MBeanInfoAssemblerAPI,即 负责定义每个公开的 bean 的 Management 接口。 默认实现org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler, 定义公开所有公共属性和方法的管理接口 (如您在前面部分的示例中看到的那样)。Spring 提供两个 其他MBeanInfoAssembler界面,让您 使用源级元数据控制生成的管理界面 或任何任意接口。spring-doc.cadn.net.cn

使用源级元数据:Java 标注

通过使用MetadataMBeanInfoAssembler中,您可以定义 使用 source level metadata 的 bean。元数据的读取由org.springframework.jmx.export.metadata.JmxAttributeSource接口。Spring JMX 提供了一个使用 Java 注解的默认实现,即org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource.您必须 配置MetadataMBeanInfoAssembler使用JmxAttributeSource接口使其正常运行,因为没有默认值。spring-doc.cadn.net.cn

要将 Bean 标记为导出到 JMX,您应该使用@ManagedResource注解。您必须将您希望公开的每个方法注释为 作与@ManagedOperation注释并注释您希望的每个属性 expose 替换为@ManagedAttribute注解。在注释属性时,您可以省略 getter 或 setter 的注解,用于创建 write-only 或 read-only 属性。spring-doc.cadn.net.cn

一个@ManagedResource-annotated bean 必须是 public,公开 作或属性。

以下示例显示了JmxTestBean类,我们 用于创建 MBeanServerspring-doc.cadn.net.cn

package org.springframework.jmx;

@ManagedResource(
		objectName="bean:name=testBean4",
		description="My Managed Bean",
		log=true,
		logFile="jmx.log",
		currencyTimeLimit=15,
		persistPolicy="OnUpdate",
		persistPeriod=200,
		persistLocation="foo",
		persistName="bar")
public class AnnotationTestBean {

	private int age;
	private String name;

	public void setAge(int age) {
		this.age = age;
	}

	@ManagedAttribute(description="The Age Attribute", currencyTimeLimit=15)
	public int getAge() {
		return this.age;
	}

	@ManagedAttribute(description="The Name Attribute",
			currencyTimeLimit=20,
			defaultValue="bar",
			persistPolicy="OnUpdate")
	public void setName(String name) {
		this.name = name;
	}

	@ManagedAttribute(defaultValue="foo", persistPeriod=300)
	public String getName() {
		return this.name;
	}

	@ManagedOperation(description="Add two numbers")
	@ManagedOperationParameter(name = "x", description = "The first number")
	@ManagedOperationParameter(name = "y", description = "The second number")
	public int add(int x, int y) {
		return x + y;
	}

	public void dontExposeMe() {
		throw new RuntimeException();
	}

}

在前面的示例中,您可以看到AnnotationTestBeanclass 被注解 跟@ManagedResource而这个@ManagedResource配置了 annotation 具有一组属性。这些属性可用于配置各个方面 由MBeanExporter并在 Greater detail 在 Spring JMX 注释中。spring-doc.cadn.net.cn

agename属性使用@ManagedAttribute, 但是,在ageproperty,则仅对 getter 方法进行注释。 这会导致这两个属性都包含在管理界面中 作为托管属性,但age属性是只读的。spring-doc.cadn.net.cn

最后,add(int, int)method 的@ManagedOperation, 而dontExposeMe()method 不是。这会导致管理界面 仅包含一个作 (add(int, int)) 时,使用MetadataMBeanInfoAssembler.spring-doc.cadn.net.cn

AnnotationTestBeanclass 不需要实现任何 Java 接口, 因为 JMX 管理接口仅从 Comments 派生。

以下配置显示了如何配置MBeanExporter要使用MetadataMBeanInfoAssembler:spring-doc.cadn.net.cn

<beans>

	<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
		<property name="assembler" ref="assembler"/>
		<property name="namingStrategy" ref="namingStrategy"/>
		<property name="autodetect" value="true"/>
	</bean>

	<!-- will create management interface using annotation metadata -->
	<bean id="assembler"
			class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
		<property name="attributeSource" ref="jmxAttributeSource"/>
	</bean>

	<!-- will pick up the ObjectName from the annotation -->
	<bean id="namingStrategy"
			class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
		<property name="attributeSource" ref="jmxAttributeSource"/>
	</bean>

	<bean id="jmxAttributeSource"
			class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>

	<bean id="testBean" class="org.springframework.jmx.AnnotationTestBean">
		<property name="name" value="TEST"/>
		<property name="age" value="100"/>
	</bean>

</beans>

在前面的示例中,MetadataMBeanInfoAssemblerbean 已配置了 实例的AnnotationJmxAttributeSource类并传递给MBeanExporter通过 assembler 属性。这就是利用所需的全部内容 用于 Spring 公开的 MBean 的 Comments 驱动的管理界面。spring-doc.cadn.net.cn

Spring JMX 注释

下表描述了可在 Spring JMX 中使用的 Comments:spring-doc.cadn.net.cn

表 1.Spring JMX 注释
注解 适用于 描述

@ManagedResourcespring-doc.cadn.net.cn

spring-doc.cadn.net.cn

标记Class作为 JMX 托管资源。spring-doc.cadn.net.cn

@ManagedNotificationspring-doc.cadn.net.cn

spring-doc.cadn.net.cn

指示托管资源发出的 JMX 通知。spring-doc.cadn.net.cn

@ManagedAttributespring-doc.cadn.net.cn

方法(仅限 getter 和 setter)spring-doc.cadn.net.cn

将 getter 或 setter 标记为 JMX 属性的一半。spring-doc.cadn.net.cn

@ManagedMetricspring-doc.cadn.net.cn

方法(仅限 getter)spring-doc.cadn.net.cn

将 getter 标记为 JMX 属性,并添加 descriptor 属性以指示它是一个量度。spring-doc.cadn.net.cn

@ManagedOperationspring-doc.cadn.net.cn

方法spring-doc.cadn.net.cn

将方法标记为 JMX作。spring-doc.cadn.net.cn

@ManagedOperationParameterspring-doc.cadn.net.cn

方法spring-doc.cadn.net.cn

定义作参数的描述。spring-doc.cadn.net.cn

下表描述了一些可用于 这些注释。有关更多详细信息,请参阅每个注释的 Javadoc。spring-doc.cadn.net.cn

表 2.Spring JMX 注释属性
属性 适用于 描述

objectNamespring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

使用者MetadataNamingStrategy要确定ObjectName的托管资源。spring-doc.cadn.net.cn

descriptionspring-doc.cadn.net.cn

@ManagedResource,@ManagedNotification,@ManagedAttribute,@ManagedMetric,@ManagedOperation,@ManagedOperationParameterspring-doc.cadn.net.cn

设置资源、通知、属性、指标或作的描述。spring-doc.cadn.net.cn

currencyTimeLimitspring-doc.cadn.net.cn

@ManagedResource,@ManagedAttribute,@ManagedMetricspring-doc.cadn.net.cn

设置currencyTimeLimitdescriptor 字段。spring-doc.cadn.net.cn

defaultValuespring-doc.cadn.net.cn

@ManagedAttributespring-doc.cadn.net.cn

设置defaultValuedescriptor 字段。spring-doc.cadn.net.cn

logspring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置logdescriptor 字段。spring-doc.cadn.net.cn

logFilespring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置logFiledescriptor 字段。spring-doc.cadn.net.cn

persistPolicyspring-doc.cadn.net.cn

@ManagedResource,@ManagedMetricspring-doc.cadn.net.cn

设置persistPolicydescriptor 字段。spring-doc.cadn.net.cn

persistPeriodspring-doc.cadn.net.cn

@ManagedResource,@ManagedMetricspring-doc.cadn.net.cn

设置persistPerioddescriptor 字段。spring-doc.cadn.net.cn

persistLocationspring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置persistLocationdescriptor 字段。spring-doc.cadn.net.cn

persistNamespring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置persistNamedescriptor 字段。spring-doc.cadn.net.cn

namespring-doc.cadn.net.cn

@ManagedOperationParameterspring-doc.cadn.net.cn

设置作参数的显示名称。spring-doc.cadn.net.cn

indexspring-doc.cadn.net.cn

@ManagedOperationParameterspring-doc.cadn.net.cn

设置作参数的索引。spring-doc.cadn.net.cn

使用AutodetectCapableMBeanInfoAssembler接口

为了进一步简化配置,Spring 包括AutodetectCapableMBeanInfoAssembler接口,该接口扩展了MBeanInfoAssembler接口添加对 MBean 资源自动检测的支持。如果配置MBeanExporter实例为AutodetectCapableMBeanInfoAssembler是的 允许 “投票” 包含用于 JMX 的 bean。spring-doc.cadn.net.cn

唯一的AutodetectCapableMBeanInfointerface 是 这MetadataMBeanInfoAssembler,该 bean 将投票以包括任何标记为 使用ManagedResource属性。在这种情况下,默认方法是使用 bean 名称作为ObjectName,这将产生类似于以下内容的配置:spring-doc.cadn.net.cn

<beans>

	<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
		<!-- notice how no 'beans' are explicitly configured here -->
		<property name="autodetect" value="true"/>
		<property name="assembler" ref="assembler"/>
	</bean>

	<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
		<property name="attributeSource">
			<bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
		</property>
	</bean>

	<bean id="testBean" class="org.springframework.jmx.AnnotationTestBean">
		<property name="name" value="TEST"/>
		<property name="age" value="100"/>
	</bean>

</beans>

请注意,在前面的配置中,没有 bean 传递给MBeanExporter. 但是,AnnotationTestBean仍然已注册,因为它使用@ManagedResourceMetadataMBeanInfoAssembler检测到此情况并投票包括 它。这种方法的唯一缺点是AnnotationTestBean现在 具有商业意义。您可以通过配置ObjectNamingStrategy控制ObjectName的实例 你的 Bean.您还可以看到一个使用MetadataNamingStrategy在“使用源代码级元数据:Java 注释”中。spring-doc.cadn.net.cn

使用 Java 接口定义管理接口

除了MetadataMBeanInfoAssembler,Spring 还包括InterfaceBasedMBeanInfoAssembler,它允许您约束 methods 和 属性,这些属性根据 接口。spring-doc.cadn.net.cn

尽管公开 MBean 的标准机制是使用接口和简单的 命名方案 /InterfaceBasedMBeanInfoAssembler通过以下方式扩展此功能 无需命名约定,让您使用多个接口 以及消除 bean 实现 MBean 接口的需要。spring-doc.cadn.net.cn

请考虑以下接口,该接口用于定义JmxTestBean类:spring-doc.cadn.net.cn

public interface IJmxTestBean {

	public int add(int x, int y);

	public long myOperation();

	public int getAge();

	public void setAge(int age);

	public void setName(String name);

	public String getName();

}

此接口定义作为作公开的方法和属性,并且 属性。以下代码显示了如何配置 Spring JMX 以使用 此接口作为 Management Interface 的定义:spring-doc.cadn.net.cn

<beans>

	<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
		<property name="beans">
			<map>
				<entry key="bean:name=testBean5" value-ref="testBean"/>
			</map>
		</property>
		<property name="assembler">
			<bean class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
				<property name="managedInterfaces">
					<value>org.springframework.jmx.IJmxTestBean</value>
				</property>
			</bean>
		</property>
	</bean>

	<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
		<property name="name" value="TEST"/>
		<property name="age" value="100"/>
	</bean>

</beans>

在前面的示例中,InterfaceBasedMBeanInfoAssembler配置为使用IJmxTestBean接口。是的 请务必了解InterfaceBasedMBeanInfoAssembler不需要实现用于生成 JMX 管理的接口 接口。spring-doc.cadn.net.cn

在上述情况下,IJmxTestBeaninterface 用于构建所有管理 所有 bean 的接口。在许多情况下,这不是所需的行为,您可能会 希望对不同的 bean 使用不同的接口。在这种情况下,您可以将InterfaceBasedMBeanInfoAssembler一个Properties实例通过interfaceMappings属性,其中每个条目的键是 Bean 名称,每个条目的值是一个 用于该 bean 的接口名称的逗号分隔列表。spring-doc.cadn.net.cn

如果未通过managedInterfacesinterfaceMappingsproperties 中,InterfaceBasedMBeanInfoAssembler反映 在 Bean 上,并使用该 Bean 实现的所有接口来创建 管理界面。spring-doc.cadn.net.cn

MethodNameBasedMBeanInfoAssembler

MethodNameBasedMBeanInfoAssembler用于指定方法名称列表 作为属性和作向 JMX 公开。下面的代码显示了一个示例 配置:spring-doc.cadn.net.cn

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
	<property name="beans">
		<map>
			<entry key="bean:name=testBean5" value-ref="testBean"/>
		</map>
	</property>
	<property name="assembler">
		<bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
			<property name="managedMethods">
				<value>add,myOperation,getName,setName,getAge</value>
			</property>
		</bean>
	</property>
</bean>

在前面的示例中,您可以看到addmyOperation方法作为 JMX 公开 作和getName(),setName(String)getAge()公开为 适当的 JMX 属性的一半。在上面的代码中,方法映射适用于 向 JMX 公开的 bean。要逐个 bean 控制方法公开,可以使用 这methodMappings的属性MethodNameMBeanInfoAssembler将 Bean 名称映射到 方法名称列表。spring-doc.cadn.net.cn