将 JMX 的 XML Spring 配置转换为 Java 配置

发布于 2024-12-02 01:31:29 字数 2664 浏览 0 评论 0原文

我有一个小型测试应用程序,用于使用 Spring 将“Bean”暴露给 JMX。它使用基于 XML 的配置,一切正常:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.dmclaughlin.spring" />
<context:property-placeholder location="classpath:test.properties"/>

<bean id="SimpleJmxController" class="com.dmclaughlin.spring.jmx.SimpleJmxBean">
    <property name="activated" value="${some.activated}"/>
</bean>

<!--  Spring JMX -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
  <property name="autodetect" value="true"></property>
  <property name="namingStrategy" ref="namingStrategy"></property>
  <property name="assembler" ref="assembler"></property>
</bean>
<bean id="attributeSource"
 class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="assembler"
 class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
  <property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="namingStrategy"
 class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
  <property name="attributeSource" ref="attributeSource"/>
</bean>

但我需要添加此功能的应用程序使用 @Configuration 样式,并且我正在尝试将上述 XML 转换为工作。我添加了这样的内容:

@Bean
public MetadataNamingStrategy getNamingStrategy() {
    MetadataNamingStrategy strategy = new MetadataNamingStrategy();
    strategy.setAttributeSource(new AnnotationJmxAttributeSource());
    return strategy;
}

@Bean
public MetadataMBeanInfoAssembler getMbeanInfoAssembler() {
    return new MetadataMBeanInfoAssembler(new AnnotationJmxAttributeSource());
}

@Bean
public MBeanExporter getExporter() {
    MBeanExporter exporter = new MBeanExporter();
    exporter.setAutodetect(true);
    exporter.setNamingStrategy(getNamingStrategy());
    exporter.setAssembler(getMbeanInfoAssembler());
    return exporter;
}    

一切都会编译,但是当我加载 JConsole 时,我的 Bean 用 @ManagedResource 和 @ManagedAttribute 注释并没有公开。我在这里缺少一些简单的东西吗?

编辑:下面的答案没有解决我的问题(问题是我在 Tomcat 环境中测试我的 XML,但在独立应用程序中测试我的非 XML 配置,这意味着没有 JMXServer现在.. d'oh),但是一旦我调试了我搞砸的东西,它确实帮助我简化了。

I have a small test app for exposing a "Bean" to JMX using Spring. It uses an XML based config and everything works fine:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.dmclaughlin.spring" />
<context:property-placeholder location="classpath:test.properties"/>

<bean id="SimpleJmxController" class="com.dmclaughlin.spring.jmx.SimpleJmxBean">
    <property name="activated" value="${some.activated}"/>
</bean>

<!--  Spring JMX -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
  <property name="autodetect" value="true"></property>
  <property name="namingStrategy" ref="namingStrategy"></property>
  <property name="assembler" ref="assembler"></property>
</bean>
<bean id="attributeSource"
 class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="assembler"
 class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
  <property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="namingStrategy"
 class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
  <property name="attributeSource" ref="attributeSource"/>
</bean>

But the application I need to add this functionality to, uses @Configuration style, and I'm trying to convert the above XML to work. I added something like this:

@Bean
public MetadataNamingStrategy getNamingStrategy() {
    MetadataNamingStrategy strategy = new MetadataNamingStrategy();
    strategy.setAttributeSource(new AnnotationJmxAttributeSource());
    return strategy;
}

@Bean
public MetadataMBeanInfoAssembler getMbeanInfoAssembler() {
    return new MetadataMBeanInfoAssembler(new AnnotationJmxAttributeSource());
}

@Bean
public MBeanExporter getExporter() {
    MBeanExporter exporter = new MBeanExporter();
    exporter.setAutodetect(true);
    exporter.setNamingStrategy(getNamingStrategy());
    exporter.setAssembler(getMbeanInfoAssembler());
    return exporter;
}    

And everything compiles, but when I load up JConsole my Bean annotated with @ManagedResource and @ManagedAttribute isn't exposed. Am I missing something simple here?

Edit: the answer below didn't fix my problem (the problem was I was testing my XML in a Tomcat environment, but testing my non-XML config in a standalone application, which meant there was no JMXServer present.. d'oh), but it did help me simplify once I debugged what I messed up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少跟Wǒ拽 2024-12-09 01:31:29

对于来说,添加就足够了:

@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
    return new AnnotationMBeanExporter();
}

For me it was enough to add:

@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
    return new AnnotationMBeanExporter();
}
被你宠の有点坏 2024-12-09 01:31:29

您应该使用“热切”

@Bean
@Lazy(false)
public MBeanExporter getExporter() {
...
}

问候

AccLess配置您的 mbeanexporter

you should configure your mbeanexporter with "eager"

@Bean
@Lazy(false)
public MBeanExporter getExporter() {
...
}

greetings

AccLess

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文