Spring-Data:使用基于java的配置</mongo:jmx>定义<mongo:jmx>

时间:2015-02-27 14:37:28

标签: spring-data spring-data-mongodb spring-java-config

使用spring data mongo时,如何在基于java的配置中执行<mongo:jmx />

1 个答案:

答案 0 :(得分:2)

Spring不支持(但是?)支持这一点。您有两种选择:

  1. 在Java Config中包含所有bean
  2. 在MongoJmxParser.registerJmxComponents()中可以找到豆。

      @Bean
    public MBeanExporter sportsbookMBeanExporter() throws MalformedObjectNameException {
        MBeanExporter exporter = new MBeanExporter();
        exporter.setAssembler(new SimpleReflectiveMBeanInfoAssembler());
        exporter.setNamingStrategy(new MBeanObjectNamingStrategy());
        Map<String, Object> beanMap = new HashMap<>();
        beanMap.put("AssertMetrics", AssertMetrics.class);
        beanMap.put("BackgroundFlushingMetrics", BackgroundFlushingMetrics.class);
        beanMap.put("BtreeIndexCounters", BtreeIndexCounters.class);
        beanMap.put("ConnectionMetrics", ConnectionMetrics.class);
        beanMap.put("GlobalLockMetrics", GlobalLockMetrics.class);
        beanMap.put("MemoryMetrics", MemoryMetrics.class);
        beanMap.put("OperationCounters", OperationCounters.class);
        beanMap.put("ServerInfo", ServerInfo.class);
        beanMap.put("MongoAdmin", MongoAdmin.class);
        exporter.setBeans(beanMap);
        return exporter;
    }
    
    1. 创建一个最小的xml-config
    2. 建议使用bean,因为新版本可能会更改。

      在JavaConfig中:

      @ImportResource("classpath:spring.xml")
      

      在src / main / resources下添加一个文件spring.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:mongo="http://www.springframework.org/schema/data/mongo"
             xsi:schemaLocation="
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
          <!--not possible to enable in Java Config-->
          <mongo:jmx/>
      </beans>