只是在本地 tomcat 服务器上的 eclipse 中尝试一个简单的 Spring JMX 应用程序,但似乎无法注册 mbean,因此它们可以在 jconsole 中查看,在 eclipse 上下文中:component-scan 似乎确实拾取了 beans我创建了但是这些没有注册。当以编程方式注册 mbean 时,它会起作用。
这是我的配置 xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="mbeanServer" class="java.lang.management.ManagementFactory"
lazy-init="false" factory-method="getPlatformMBeanServer">
</bean>
<context:component-scan base-package="com.jmx.beans" />
<context:mbean-export server="mbeanServer" />
</beans>
我正在尝试使用注解注册的简单 bean
package com.jmx.beans;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;
@Component
@ManagedResource(objectName="bean:name=Hello")
public class Hello{
String message = null;
@ManagedAttribute(description="get the message")
public String getMessage(){
return this.message;
}
@ManagedAttribute(description="set the message")
public void setMessage(String Message){
this.message = Message;
}
}
我还设置了 tomcat 服务器参数如下
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9990
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.hostname="localhost"
如有任何帮助,我们将不胜感激,谢谢
最佳答案
您为什么要编辑帖子以删除 <context:component-scan/>
?这是找到您的 @Component
所必需的.
我刚刚测试过,对我来说一切正常......
@Component
@ManagedResource
public class Foo {
@ManagedAttribute
public int getIt() {
return 42;
}
}
和
<context:mbean-server/>
<context:component-scan base-package="foo" />
<context:mbean-export/>
我用你的 MBean 服务器风格试过了,也很管用。
关于Spring JMX Mbean 未出现或未在 jconsole 中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15411321/