Преобразование конфигурации XML Spring для JMX в конфигурацию Java - PullRequest
2 голосов
/ 26 августа 2011

У меня есть небольшое тестовое приложение для демонстрации "Bean" JMX с помощью Spring. Он использует конфигурацию на основе 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, мой Бин с аннотациями @ManagedResource и @ManagedAttribute не раскрывается. Я что-то упускаю здесь?

Редактировать : приведенный ниже ответ не решил мою проблему (проблема заключалась в том, что я тестировал свой XML в среде Tomcat, но тестировал конфигурацию, отличную от XML, в отдельном приложении, что означало, что JMXServer отсутствует .. да), но это помогло мне упростить процесс отладки.

Ответы [ 2 ]

3 голосов
/ 26 августа 2011

Для меня достаточно было добавить:

@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
    return new AnnotationMBeanExporter();
}
0 голосов
/ 13 августа 2014

вы должны настроить свой mbeanexporter на "eager"

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

привет

AccLess

...