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

控制 Bean 的管理界面

上一节的示例中, 您对 Bean 的管理界面几乎没有控制权。所有public每个导出的 bean 的属性和方法都公开为 JMX 属性,并且 作。对确切的控制进行更细粒度的控制 导出的 bean 的属性和方法实际上作为 JMX 属性公开 和作,Spring JMX 提供了一个全面且可扩展的机制 控制 Bean 的管理接口。spring-doc.cadn.net.cn

使用MBeanInfoAssembler应用程序接口

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

使用源级元数据:Java 注释

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

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

一个@ManagedResource-带注释的 bean 必须是公共的,公开的方法也必须是公共的 作或属性。

以下示例显示了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配置注释 具有一组属性。这些属性可用于配置各个方面 由MBeanExporter并在 greater 中解释 稍后在 Spring JMX Annotations 中详细介绍。spring-doc.cadn.net.cn

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

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

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

以下配置显示了如何配置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通过汇编器属性。这就是利用所需的全部内容 Spring 公开的 MBean 的注释驱动管理接口。spring-doc.cadn.net.cn

Spring JMX 注释

下表描述了可用于 Spring JMX 的注释: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 属性,并添加描述符属性以指示它是指标。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

设置currencyTimeLimit描述符字段。spring-doc.cadn.net.cn

defaultValuespring-doc.cadn.net.cn

@ManagedAttributespring-doc.cadn.net.cn

设置defaultValue描述符字段。spring-doc.cadn.net.cn

logspring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置log描述符字段。spring-doc.cadn.net.cn

logFilespring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置logFile描述符字段。spring-doc.cadn.net.cn

persistPolicyspring-doc.cadn.net.cn

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

设置persistPolicy描述符字段。spring-doc.cadn.net.cn

persistPeriodspring-doc.cadn.net.cn

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

设置persistPeriod描述符字段。spring-doc.cadn.net.cn

persistLocationspring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置persistLocation描述符字段。spring-doc.cadn.net.cn

persistNamespring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

设置persistName描述符字段。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

唯一的实现AutodetectCapableMBeanInfo接口是 这MetadataMBeanInfoAssembler,它投票以包含任何标记的 Bean 使用ManagedResource属性。在这种情况下,默认方法是使用 bean name 作为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实例 你的豆子.您还可以看到一个使用MetadataNamingStrategy使用源级元数据:Java 注释中。spring-doc.cadn.net.cn

使用 Java 接口定义管理接口

除了MetadataMBeanInfoAssembler,Spring 还包括InterfaceBasedMBeanInfoAssembler,这允许您约束方法 基于集合中定义的方法集公开的属性 接口。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();

}

此接口定义作为作公开的方法和属性,并且 JMX MBean 上的属性。以下代码显示了如何配置 Spring JMX 以使用 此接口作为管理接口的定义: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

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

如果未通过managedInterfacesinterfaceMappings属性,则InterfaceBasedMBeanInfoAssembler反映 并使用该 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