上一节的示例中, 您几乎无法控制 Bean 的管理界面。每个导出的 Bean 的所有属性和方法都作为 JMX 属性和 操作。对哪个进行更细粒度的控制 导出的 Bean 的属性和方法实际上作为 JMX 属性公开 和操作,Spring JMX 提供了一个全面且可扩展的机制 控制 Bean 的管理界面。publicSpring中文文档

使用界面MBeanInfoAssembler

在幕后,委托给接口的实现,即 负责定义每个公开的 Bean 的管理接口。 默认实现, 定义一个公开所有公共属性和方法的管理接口 (如您在前面各节的示例中看到的那样)。Spring 提供两个 接口的其他实现,可让您 使用任一源级元数据控制生成的管理界面 或任何任意接口。MBeanExporterorg.springframework.jmx.export.assembler.MBeanInfoAssemblerorg.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssemblerMBeanInfoAssemblerSpring中文文档

使用源代码级元数据:Java 注释

通过使用 ,可以定义管理接口 对于您的 Bean,使用源级元数据。元数据的读取被封装 通过接口。 Spring JMX 提供了一个使用 Java 注解的默认实现,即 . 您必须使用 它正常运行的接口(没有默认值)。MetadataMBeanInfoAssemblerorg.springframework.jmx.export.metadata.JmxAttributeSourceorg.springframework.jmx.export.annotation.AnnotationJmxAttributeSourceMetadataMBeanInfoAssemblerJmxAttributeSourceSpring中文文档

要标记要导出到 JMX 的 Bean,您应该使用注释对 Bean 类进行注释。必须将要公开的每个方法标记为操作 使用注释并标记要公开的每个属性 带有注释。标记属性时,可以省略 用于创建只写或只读的 getter 或 setter 的注释 属性。ManagedResourceManagedOperationManagedAttributeSpring中文文档

带注释的 Bean 必须是公共的,公开的方法也必须是公共的 操作或属性。ManagedResource

下面的示例显示了我们 用于创建 MBeanServerJmxTestBeanSpring中文文档

package org.springframework.jmx;

import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedAttribute;

@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 implements IJmxTestBean {

	private String name;
	private int age;

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

	public void setAge(int age) {
		this.age = 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 name;
	}

	@ManagedOperation(description="Add two numbers")
	@ManagedOperationParameters({
		@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();
	}

}

在前面的示例中,您可以看到该类标有批注,并且配置了此批注 具有一组属性。这些属性可用于配置各个方面 的 MBean 由 生成 并且以更大的方式解释 稍后在源级元数据类型中进行了详细介绍。JmxTestBeanManagedResourceManagedResourceMBeanExporterSpring中文文档

和 属性都用注释进行注释,但是,对于属性,只标记 getter。 这会导致这两个属性都包含在管理界面中 作为属性,但该属性是只读的。agenameManagedAttributeageageSpring中文文档

最后,该方法用属性进行标记, 而方法不是。这会导致管理界面 当您使用 .add(int, int)ManagedOperationdontExposeMe()add(int, int)MetadataMBeanInfoAssemblerSpring中文文档

以下配置显示了如何配置以使用 :MBeanExporterMetadataMBeanInfoAssemblerSpring中文文档

<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>

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

	<!-- 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="testBean" class="org.springframework.jmx.AnnotationTestBean">
		<property name="name" value="TEST"/>
		<property name="age" value="100"/>
	</bean>
</beans>

在前面的示例中,一个 Bean 配置了 类的实例,并传递给 Through the Assembler 属性。这就是利用所需的全部内容 元数据驱动的管理界面,用于 Spring 公开的 MBean。MetadataMBeanInfoAssemblerAnnotationJmxAttributeSourceMBeanExporterSpring中文文档

带注释的 Bean 必须是公共的,公开的方法也必须是公共的 操作或属性。ManagedResource

源级元数据类型

下表描述了可用于 Spring JMX 的源级元数据类型:Spring中文文档

表 1.源级元数据类型
目的 注解 注释类型

将 a 的所有实例标记为 JMX 托管资源。ClassSpring中文文档

@ManagedResourceSpring中文文档

Spring中文文档

将方法标记为 JMX 操作。Spring中文文档

@ManagedOperationSpring中文文档

方法Spring中文文档

将 getter 或 setter 标记为 JMX 属性的一半。Spring中文文档

@ManagedAttributeSpring中文文档

方法(仅限 getter 和 setter)Spring中文文档

定义操作参数的描述。Spring中文文档

@ManagedOperationParameter@ManagedOperationParametersSpring中文文档

方法Spring中文文档

下表描述了可用于这些源代码级别的配置参数 元数据类型:Spring中文文档

表 2.源级元数据参数
参数 描述 适用于

ObjectNameSpring中文文档

用于确定托管资源的 。MetadataNamingStrategyObjectNameSpring中文文档

ManagedResourceSpring中文文档

descriptionSpring中文文档

设置资源、属性或操作的友好描述。Spring中文文档

ManagedResourceManagedAttributeManagedOperationManagedOperationParameterSpring中文文档

currencyTimeLimitSpring中文文档

设置描述符字段的值。currencyTimeLimitSpring中文文档

ManagedResourceManagedAttributeSpring中文文档

defaultValueSpring中文文档

设置描述符字段的值。defaultValueSpring中文文档

ManagedAttributeSpring中文文档

logSpring中文文档

设置描述符字段的值。logSpring中文文档

ManagedResourceSpring中文文档

logFileSpring中文文档

设置描述符字段的值。logFileSpring中文文档

ManagedResourceSpring中文文档

persistPolicySpring中文文档

设置描述符字段的值。persistPolicySpring中文文档

ManagedResourceSpring中文文档

persistPeriodSpring中文文档

设置描述符字段的值。persistPeriodSpring中文文档

ManagedResourceSpring中文文档

persistLocationSpring中文文档

设置描述符字段的值。persistLocationSpring中文文档

ManagedResourceSpring中文文档

persistNameSpring中文文档

设置描述符字段的值。persistNameSpring中文文档

ManagedResourceSpring中文文档

nameSpring中文文档

设置操作参数的显示名称。Spring中文文档

ManagedOperationParameterSpring中文文档

indexSpring中文文档

设置操作参数的索引。Spring中文文档

ManagedOperationParameterSpring中文文档

表 1.源级元数据类型
目的 注解 注释类型

将 a 的所有实例标记为 JMX 托管资源。ClassSpring中文文档

@ManagedResourceSpring中文文档

Spring中文文档

将方法标记为 JMX 操作。Spring中文文档

@ManagedOperationSpring中文文档

方法Spring中文文档

将 getter 或 setter 标记为 JMX 属性的一半。Spring中文文档

@ManagedAttributeSpring中文文档

方法(仅限 getter 和 setter)Spring中文文档

定义操作参数的描述。Spring中文文档

@ManagedOperationParameter@ManagedOperationParametersSpring中文文档

方法Spring中文文档

表 2.源级元数据参数
参数 描述 适用于

ObjectNameSpring中文文档

用于确定托管资源的 。MetadataNamingStrategyObjectNameSpring中文文档

ManagedResourceSpring中文文档

descriptionSpring中文文档

设置资源、属性或操作的友好描述。Spring中文文档

ManagedResourceManagedAttributeManagedOperationManagedOperationParameterSpring中文文档

currencyTimeLimitSpring中文文档

设置描述符字段的值。currencyTimeLimitSpring中文文档

ManagedResourceManagedAttributeSpring中文文档

defaultValueSpring中文文档

设置描述符字段的值。defaultValueSpring中文文档

ManagedAttributeSpring中文文档

logSpring中文文档

设置描述符字段的值。logSpring中文文档

ManagedResourceSpring中文文档

logFileSpring中文文档

设置描述符字段的值。logFileSpring中文文档

ManagedResourceSpring中文文档

persistPolicySpring中文文档

设置描述符字段的值。persistPolicySpring中文文档

ManagedResourceSpring中文文档

persistPeriodSpring中文文档

设置描述符字段的值。persistPeriodSpring中文文档

ManagedResourceSpring中文文档

persistLocationSpring中文文档

设置描述符字段的值。persistLocationSpring中文文档

ManagedResourceSpring中文文档

persistNameSpring中文文档

设置描述符字段的值。persistNameSpring中文文档

ManagedResourceSpring中文文档

nameSpring中文文档

设置操作参数的显示名称。Spring中文文档

ManagedOperationParameterSpring中文文档

indexSpring中文文档

设置操作参数的索引。Spring中文文档

ManagedOperationParameterSpring中文文档

使用界面AutodetectCapableMBeanInfoAssembler

为了进一步简化配置,Spring 包含了接口,该接口扩展了接口以添加对 MBean 资源自动检测的支持。如果使用 的实例配置 ,则 允许“投票”是否包含用于接触 JMX 的 bean。AutodetectCapableMBeanInfoAssemblerMBeanInfoAssemblerMBeanExporterAutodetectCapableMBeanInfoAssemblerSpring中文文档

该接口的唯一实现是 的 ,它投票决定包含任何被标记的 Bean 替换为属性。在这种情况下,默认方法是使用 Bean 名称为 ,这将导致类似于以下内容的配置:AutodetectCapableMBeanInfoMetadataMBeanInfoAssemblerManagedResourceObjectNameSpring中文文档

<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="testBean" class="org.springframework.jmx.JmxTestBean">
		<property name="name" value="TEST"/>
		<property name="age" value="100"/>
	</bean>

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

</beans>

请注意,在前面的配置中,没有 Bean 传递给 . 但是,它仍然被注册,因为它被标记为属性,并且检测到这一点并投票包含它。 这种方法的唯一问题是现在的名称有业务 意义。您可以通过更改 Controlling ObjectName Instances for Your Bean 中定义的默认创建行为来解决此问题。MBeanExporterJmxTestBeanManagedResourceMetadataMBeanInfoAssemblerJmxTestBeanObjectNameSpring中文文档

使用 Java 接口定义管理接口

除了 ,Spring 还包括 ,它允许您约束方法和 基于集合中定义的方法集公开的属性 接口。MetadataMBeanInfoAssemblerInterfaceBasedMBeanInfoAssemblerSpring中文文档

虽然公开 MBean 的标准机制是使用接口和简单的 命名方案,通过以下方式扩展此功能 无需命名约定,允许您使用多个接口 并且无需 Bean 来实现 MBean 接口。InterfaceBasedMBeanInfoAssemblerSpring中文文档

请考虑以下接口,该接口用于为我们前面演示的类定义管理接口:JmxTestBeanSpring中文文档

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();

}

此接口定义作为操作和 JMX MBean 上的属性。以下代码演示如何配置 Spring JMX 以 此接口作为管理接口的定义:Spring中文文档

<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>

在前面的示例中,配置为在为任何 Bean 构建管理接口时使用该接口。是的 重要的是要了解 be 处理的 bean 不需要实现用于生成 JMX 管理的接口 接口。InterfaceBasedMBeanInfoAssemblerIJmxTestBeanInterfaceBasedMBeanInfoAssemblerSpring中文文档

在上述情况下,该接口用于构造所有管理 所有 Bean 的接口。在许多情况下,这不是所需的行为,您可以 想要对不同的 Bean 使用不同的接口。在本例中,您可以通过属性传递实例,其中每个条目的键是 Bean 名称,每个条目的值是 要用于该 Bean 的接口名称的逗号分隔列表。IJmxTestBeanInterfaceBasedMBeanInfoAssemblerPropertiesinterfaceMappingsSpring中文文档

如果未通过 or 属性指定管理接口,则 并使用该 Bean 实现的所有接口来创建 管理界面。managedInterfacesinterfaceMappingsInterfaceBasedMBeanInfoAssemblerSpring中文文档

MethodNameBasedMBeanInfoAssembler

MethodNameBasedMBeanInfoAssembler允许您指定方法名称列表 作为属性和操作向 JMX 公开。下面的代码显示了一个示例 配置:Spring中文文档

<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>

在前面的示例中,您可以看到 and 方法公开为 JMX 操作、 和 、 和 公开为 JMX 属性的适当一半。在前面的代码中,方法映射适用于 暴露给 JMX 的 Bean。要逐个 Bean 控制方法暴露,您可以使用 将 Bean 名称映射到 的属性 方法名称列表。addmyOperationgetName()setName(String)getAge()methodMappingsMethodNameMBeanInfoAssemblerSpring中文文档