Преобразование конфигурации XML Spring для JMX в конфигурацию Java

У меня есть небольшое тестовое приложение для демонстрации "Боба" 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>

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

И все компилируется, но когда я загружаю JConsole, мой Bean, аннотированный @ManagedResource и @ManagedAttribute, не отображается. Я пропустил что-то простое здесь?

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


person David McLaughlin    schedule 26.08.2011    source источник
comment
твоя проблема решена   -  person Bhupi    schedule 29.10.2015


Ответы (2)


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

@Bean
public AnnotationMBeanExporter annotationMBeanExporter() {
    return new AnnotationMBeanExporter();
}
person Tomasz Nurkiewicz    schedule 26.08.2011
comment
Спасибо, но мне это тоже не подходит. У меня есть ServiceImpl с аннотацией @ManagedResource, и его находит IoCProvider.. поэтому я не уверен, почему он не отображается в jConsole. - person David McLaughlin; 26.08.2011

вы должны настроить свой mbeanexporter с «нетерпеливым»

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

Привет

доступ

person user3936864    schedule 13.08.2014