title: Cannot integrate JMX with Spring application
body: <p>I have got a <strong>SPRING</strong> application. When I run</p>
  
  <pre><code>mvn jetty:run
  </code></pre>
  
  <p>everything is ok. </p>
  
  <p>I would like to use <strong>JMX</strong> in my project. </p>
  
  <p>I created another project, I tried tutorial for beginners and I was able to see some changes with <strong>jconsole</strong>.</p>
  
  <p>Now, I want to use JMX in my real project and I would like to use SPRING libraries which manages JMX - following this post</p>
  
  <p><a href="https://stackoverflow.com/q/17010651/1021970">How to integrate JMX with Spring?</a></p>
  
  <p>I have got a class:</p>
  
  <pre><code>public class MyMainClass {
    private int var1;
    private int var2;
    private TimeUnit var3;
  
    // public getters and setters
    public static MyXXXClass&lt;String, Object&gt; getInstance();
  }
  </code></pre>
  
  <p>and in <strong>config.xml</strong></p>
  
  <pre><code>&lt;!-- other beans --&gt;
  &lt;bean id="myid" class="com.my.package.MyMainClass"&gt;
      &lt;property name="var1" value.../&gt;
      &lt;property name="var2" value... /&gt;
      &lt;property name="var3" value.../&gt;
  &lt;/bean&gt;
  &lt;!-- other beans --&gt;
  </code></pre>
  
  <p>I changed few things to make it works with JMX.</p>
  
  <p>I added an interface:</p>
  
  <pre><code>import java.util.concurrent.TimeUnit;
  
  public interface IMyMainClassBean {
  
      public int getVar1();
  
      public void setVar1(int var1);
  
      public int getVar2();
  
      public void setVar2(int var2);
  
      public TimeUnit getVar3();
  
      public void setVar3(TimeUnit var3);
  }
  </code></pre>
  
  <p>I added <strong>implements</strong> to my class:</p>
  
  <pre><code>public class MyMainClassBean implements IMyMainClassBean {...}
  </code></pre>
  
  <p>Last thing, I edited my xml file:</p>
  
  <pre><code>&lt;!-- other beans --&gt;
  &lt;bean id="myid" class="com.my.package.MyMainClassBean"&gt;
      &lt;property name="var1" value.../&gt;
      &lt;property name="var2" value... /&gt;
      &lt;property name="var3" value.../&gt;
  &lt;/bean&gt;
  
  &lt;!-- this bean must not be lazily initialized if the exporting is to happen --&gt;
  &lt;bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
      lazy-init="false"&gt;
      &lt;property name="beans"&gt;
          &lt;map&gt;
              &lt;entry key="bean:name=testBean1" value-ref="myid" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;!-- other beans --&gt;
  </code></pre>
  
  <p>Now, when I start my server, it gives me a lot of exceptions (log is really long, so I copied just a part which I think is the most important).</p>
  
  <pre><code>Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [com.my.package
  .MyMainClassBean@3d4395fb] with key 'bean:name=testBean1'; nested exception is javax.management.InstanceAlreadyExistsExcep
  tion: bean:name=testBean1
          at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:602)
          at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
          at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
  eanFactory.java:1571)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBean
  Factory.java:1509)
          ... 163 more
  Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
          at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
          at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
          at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
          at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
          at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
          ... 167 more
  [WARNING] Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myTask' defined
   in class path resource [anotherconfigfile.xml]: Cannot resolve reference to bean 'anotherimport' while setting bean property 'targetObj
  ect'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anotherimport' defin
  ed in class path resource [anotherconfigfile.xml]: Cannot create inner bean 'myTotallyAnotherClass' of type [com.my.package.another.MyTotallyAnotherClass]
   while setting bean property 'myTotallyAnotherClass'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creatin
  g bean with name 'myTotallyAnotherClass' defined in class path resource [anotherconfigfile.xml]: Instantiation of bean failed; nested exception is
   org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.my.package.another.MyTotallyAnotherClass]: Co
  nstructor threw exception; nested exception is java.lang.ExceptionInInitializerError:
  javax.management.InstanceAlreadyExistsException: bean:name=testBean1
          at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:453)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.internal_addObject(DefaultMBeanServerInterceptor.java:1484)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:963)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:917)
          at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:312)
          at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:483)
          at org.springframework.jmx.support.MBeanRegistrationSupport.doRegister(MBeanRegistrationSupport.java:195)
          at org.springframework.jmx.export.MBeanExporter.registerBeanInstance(MBeanExporter.java:655)
          at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(MBeanExporter.java:592)
          at org.springframework.jmx.export.MBeanExporter.registerBeans(MBeanExporter.java:527)
          at org.springframework.jmx.export.MBeanExporter.afterPropertiesSet(MBeanExporter.java:413)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableB
  eanFactory.java:1571)
  </code></pre>
  
  <p>I have no even idea how to debug it.</p>
  
  <p>Thank you for all your hints.</p>