title: Making a Spring Service class manageable via JMX
body: <p>This may be trivial, but I seem to continuously run at a wall and can't find my mistake.</p>
  
  <p>I am extending the Apache James 3 mailserver which used Spring and OpenJPA. I would now like to access a Service class which used a JPA Repository via JMX.</p>
  
  <p>The Service is</p>
  
  <pre><code>@Component
  @ManagedResource(objectName = "bean:name=BlacklistServiceImpl", description = "The service providing access to the blacklisted recipients in the DB.")
  public class BlacklistServiceImpl implements BlacklistService {
  
      @Autowired
      private BlackListRepository blackListRepository;
      @Autowired
      private ConversationService conversationService;
  
      @Override
      @ManagedOperation(description = "Add address to blacklist")
      @ManagedOperationParameter(name = "address", description = "Address to add to blacklist.")
      public void addAddress(String address) {
          //Stuff
      }
  
      @Override
      @ManagedOperation(description = "Remove address from blacklist")
      @ManagedOperationParameter(name = "address", description = "Address to remove from blacklist.")
      public void removeAddress(String address) {
          //Stuff
      }
  }
  </code></pre>
  
  <p>and I have added to following to my context:</p>
  
  <pre><code>&lt;bean id="mailserverBeansExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"&gt;
      &lt;property name="beans"&gt;
          &lt;map&gt;
              &lt;entry key="bean:name=BlacklistServiceImpl"&gt;
                  &lt;bean class="&lt;snip&gt;.services.blacklist.impl.BlacklistServiceImpl"/&gt;
              &lt;/entry&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>While I have managed to build this, James crashes at startup with the following exception:</p>
  
  <pre><code>    [INFO] Starting Apache James Server...
      [INFO] ------------------------------------------------------------------------
      [ERROR] FATAL ERROR
      [INFO] ------------------------------------------------------------------------
      [INFO] Error creating bean with name 'blacklistServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.B
      eanCreationException: Could not autowire field: private &lt;snip&gt;.core.repositories.blacklist.BlackListRepository &lt;snip&gt;.service
      s.blacklist.impl.BlacklistServiceImpl.blackListRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean
       of type [&lt;snip&gt;.repositories.blacklist.BlackListRepository] found for dependency: expected at least 1 bean which qualifies as autowire cand
      idate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      [INFO] ------------------------------------------------------------------------
      [INFO] Trace
      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blacklistServiceImpl': Injection of autowired dependencies failed; neste
      d exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private &lt;snip&gt;.repositories.blacklist.Blac
      kListRepository &lt;snip&gt;.services.blacklist.impl.BlacklistServiceImpl.blackListRepository; nested exception is org.springframework.beans.facto
      ry.NoSuchBeanDefinitionException: No matching bean of type [&lt;snip&gt;.repositories.blacklist.BlackListRepository] found for dependency: expecte
      d at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(re
      quired=true)}
              at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java
      :285)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
              at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
              at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
              at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
              at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
              at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
              at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
              at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
              at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)
              at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:93)
              at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
              at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
              at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
              at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
              at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
              at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
              at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
              at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
              at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
              at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
              at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
              at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
              at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
              at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private &lt;snip&gt;.repositories.blacklist.BlackLis
      tRepository &lt;snip&gt;.services.blacklist.impl.BlacklistServiceImpl.blackListRepository; nested exception is org.springframework.beans.factory.N
      oSuchBeanDefinitionException: No matching bean of type [&lt;snip&gt;.repositories.blacklist.BlackListRepository] found for dependency: expected at
       least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(requir
      ed=true)}
              at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j
      ava:502)
              at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
              at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java
      :282)
              ... 33 more
      Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [&lt;snip&gt;.repositories.blacklist.BlackLis
      tRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframe
      work.beans.factory.annotation.Autowired(required=true)}
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
              at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
              at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.j
      ava:474)
              ... 35 more
  </code></pre>
  
  <p>The BlackListRepository has two implementations: the JPABlackListRepository and the InMemoryBlackListRepository, which is used for unit testing. I tried to solve the problem by adding</p>
  
  <pre><code>&lt;bean id="BlackListRepository" class="&lt;snip&gt;.JPABlackListRepository" /&gt;
  </code></pre>
  
  <p>to the common-context, but this lead to the unit tests failing as now the in memory implementation wasn't used anymore.</p>
  
  <p>I would appreciate any help.</p>
  
  <p><em>Update:</em></p>
  
  <p>While debugging (and thanks to the comment below) I realized that only removing BOTH the @Component and @Managed* annotations as well as the section</p>
  
  <pre><code>&lt;bean id="mailserverBeansExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter"&gt;
      &lt;property name="beans"&gt;
          &lt;map&gt;
              &lt;entry key="bean:name=BlacklistServiceImpl"&gt;
                  &lt;bean class="at.willhaben.safe.sms.core.services.blacklist.impl.BlacklistServiceImpl"/&gt;
              &lt;/entry&gt;
          &lt;/map&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>leads to a clean startup. Obviously then JMX doesn't work, but leaving any of those statements in the code leads to above exception.</p>