此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.4spring-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.MBeanInfoAssembler接口,即 负责定义每个公开的 bean 的 Management 接口。 默认实现org.springframework.jmx.export.assembler.SimpleReflectiveMBeanInfoAssembler, 定义公开所有公共属性和方法的管理接口 (如您在前面部分的示例中看到的那样)。Spring 提供两个 其他MBeanInfoAssembler界面,让您 使用源级元数据控制生成的管理界面 或任何任意接口。spring-doc.cadn.net.cn

使用源级元数据:Java 标注

通过使用MetadataMBeanInfoAssembler中,您可以定义管理界面 用于 bean。元数据的读取是封装的 由org.springframework.jmx.export.metadata.JmxAttributeSource接口。 Spring JMX 提供了一个使用 Java 注释的默认实现,即org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource. 您必须配置MetadataMBeanInfoAssembler使用 这JmxAttributeSourceinterface 使其正常运行(没有默认值)。spring-doc.cadn.net.cn

要将 Bean 标记为导出到 JMX,您应该使用ManagedResource注解。您必须将要公开的每个方法标记为作 使用ManagedOperation注释并标记要公开的每个属性 使用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;

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

}

In the preceding example, you can see that the JmxTestBean class is marked with the ManagedResource annotation and that this ManagedResource annotation is configured with a set of properties. These properties can be used to configure various aspects of the MBean that is generated by the MBeanExporter and are explained in greater detail later in Source-level Metadata Types.spring-doc.cadn.net.cn

Both the age and name properties are annotated with the ManagedAttribute annotation, but, in the case of the age property, only the getter is marked. This causes both of these properties to be included in the management interface as attributes, but the age attribute is read-only.spring-doc.cadn.net.cn

Finally, the add(int, int) method is marked with the ManagedOperation attribute, whereas the dontExposeMe() method is not. This causes the management interface to contain only one operation (add(int, int)) when you use the MetadataMBeanInfoAssembler.spring-doc.cadn.net.cn

The following configuration shows how you can configure the MBeanExporter to use the 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>

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

In the preceding example, an MetadataMBeanInfoAssembler bean has been configured with an instance of the AnnotationJmxAttributeSource class and passed to the MBeanExporter through the assembler property. This is all that is required to take advantage of metadata-driven management interfaces for your Spring-exposed MBeans.spring-doc.cadn.net.cn

Source-level Metadata Types

The following table describes the source-level metadata types that are available for use in Spring JMX:spring-doc.cadn.net.cn

Table 1. Source-level metadata types
Purpose Annotation Annotation Type

Mark all instances of a Class as JMX managed resources.spring-doc.cadn.net.cn

@ManagedResourcespring-doc.cadn.net.cn

Classspring-doc.cadn.net.cn

Mark a method as a JMX operation.spring-doc.cadn.net.cn

@ManagedOperationspring-doc.cadn.net.cn

Methodspring-doc.cadn.net.cn

Mark a getter or setter as one half of a JMX attribute.spring-doc.cadn.net.cn

@ManagedAttributespring-doc.cadn.net.cn

Method (only getters and setters)spring-doc.cadn.net.cn

Define descriptions for operation parameters.spring-doc.cadn.net.cn

@ManagedOperationParameter and @ManagedOperationParametersspring-doc.cadn.net.cn

Methodspring-doc.cadn.net.cn

The following table describes the configuration parameters that are available for use on these source-level metadata types:spring-doc.cadn.net.cn

Table 2. Source-level metadata parameters
Parameter Description Applies to

ObjectNamespring-doc.cadn.net.cn

Used by MetadataNamingStrategy to determine the ObjectName of a managed resource.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

descriptionspring-doc.cadn.net.cn

Sets the friendly description of the resource, attribute or operation.spring-doc.cadn.net.cn

ManagedResource, ManagedAttribute, ManagedOperation, or ManagedOperationParameterspring-doc.cadn.net.cn

currencyTimeLimitspring-doc.cadn.net.cn

Sets the value of the currencyTimeLimit descriptor field.spring-doc.cadn.net.cn

ManagedResource or ManagedAttributespring-doc.cadn.net.cn

defaultValuespring-doc.cadn.net.cn

Sets the value of the defaultValue descriptor field.spring-doc.cadn.net.cn

ManagedAttributespring-doc.cadn.net.cn

logspring-doc.cadn.net.cn

Sets the value of the log descriptor field.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

logFilespring-doc.cadn.net.cn

Sets the value of the logFile descriptor field.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

persistPolicyspring-doc.cadn.net.cn

Sets the value of the persistPolicy descriptor field.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

persistPeriodspring-doc.cadn.net.cn

Sets the value of the persistPeriod descriptor field.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

persistLocationspring-doc.cadn.net.cn

Sets the value of the persistLocation descriptor field.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

persistNamespring-doc.cadn.net.cn

Sets the value of the persistName descriptor field.spring-doc.cadn.net.cn

ManagedResourcespring-doc.cadn.net.cn

namespring-doc.cadn.net.cn

Sets the display name of an operation parameter.spring-doc.cadn.net.cn

ManagedOperationParameterspring-doc.cadn.net.cn

indexspring-doc.cadn.net.cn

Sets the index of an operation parameter.spring-doc.cadn.net.cn

ManagedOperationParameterspring-doc.cadn.net.cn

Using the AutodetectCapableMBeanInfoAssembler Interface

To simplify configuration even further, Spring includes the AutodetectCapableMBeanInfoAssembler interface, which extends the MBeanInfoAssembler interface to add support for autodetection of MBean resources. If you configure the MBeanExporter with an instance of AutodetectCapableMBeanInfoAssembler, it is allowed to “vote” on the inclusion of beans for exposure to JMX.spring-doc.cadn.net.cn

The only implementation of the AutodetectCapableMBeanInfo interface is the MetadataMBeanInfoAssembler, which votes to include any bean that is marked with the ManagedResource attribute. The default approach in this case is to use the bean name as the ObjectName, which results in a configuration similar to the following: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="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>

Notice that, in the preceding configuration, no beans are passed to the MBeanExporter. However, the JmxTestBean is still registered, since it is marked with the ManagedResource attribute and the MetadataMBeanInfoAssembler detects this and votes to include it. The only problem with this approach is that the name of the JmxTestBean now has business meaning. You can address this issue by changing the default behavior for ObjectName creation as defined in Controlling ObjectName Instances for Your Beans.spring-doc.cadn.net.cn

Defining Management Interfaces by Using Java Interfaces

In addition to the MetadataMBeanInfoAssembler, Spring also includes the InterfaceBasedMBeanInfoAssembler, which lets you constrain the methods and properties that are exposed based on the set of methods defined in a collection of interfaces.spring-doc.cadn.net.cn

Although the standard mechanism for exposing MBeans is to use interfaces and a simple naming scheme, InterfaceBasedMBeanInfoAssembler extends this functionality by removing the need for naming conventions, letting you use more than one interface and removing the need for your beans to implement the MBean interfaces.spring-doc.cadn.net.cn

Consider the following interface, which is used to define a management interface for the JmxTestBean class that we showed earlier: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();

}

This interface defines the methods and properties that are exposed as operations and attributes on the JMX MBean. The following code shows how to configure Spring JMX to use this interface as the definition for the 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>

In the preceding example, the InterfaceBasedMBeanInfoAssembler is configured to use the IJmxTestBean interface when constructing the management interface for any bean. It is important to understand that beans processed by the InterfaceBasedMBeanInfoAssembler are not required to implement the interface used to generate the JMX management interface.spring-doc.cadn.net.cn

In the preceding case, the IJmxTestBean interface is used to construct all management interfaces for all beans. In many cases, this is not the desired behavior, and you may want to use different interfaces for different beans. In this case, you can pass InterfaceBasedMBeanInfoAssembler a Properties instance through the interfaceMappings property, where the key of each entry is the bean name and the value of each entry is a comma-separated list of interface names to use for that bean.spring-doc.cadn.net.cn

If no management interface is specified through either the managedInterfaces or interfaceMappings properties, the InterfaceBasedMBeanInfoAssembler reflects on the bean and uses all of the interfaces implemented by that bean to create the management interface.spring-doc.cadn.net.cn

Using MethodNameBasedMBeanInfoAssembler

MethodNameBasedMBeanInfoAssembler lets you specify a list of method names that are exposed to JMX as attributes and operations. The following code shows a sample configuration: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>

In the preceding example, you can see that the add and myOperation methods are exposed as JMX operations, and getName(), setName(String), and getAge() are exposed as the appropriate half of a JMX attribute. In the preceding code, the method mappings apply to beans that are exposed to JMX. To control method exposure on a bean-by-bean basis, you can use the methodMappings property of MethodNameMBeanInfoAssembler to map bean names to lists of method names.spring-doc.cadn.net.cn