title: How to inject Prometheus metrics bean in Spring Boot Application?
body: <p>I have certain classes to which I want to inject bean from Prometheus Config as a constructor parameter.</p>
  
  <pre><code>@Component
  class Foo(
      private val counterMetric: Metric
  ) {
  
  }
  
  @Component
  class Bar(
      private val counterMetric: Metric
  ) {
  
  }
  
  </code></pre>
  
  <pre><code>@Configuration
  class PrometheusConfig {
  
      @Bean
      fun counterMetric() = Counter.build()
              .name("invite_requests")
              .help("Number of invite requests")
              .labelNames("method")
              .register()
  }
  </code></pre>
  
  <p>I can't inject this bean, because Spring Boot has @PrometheusMetricsExportAutoConfiguration annotation. How I can deal with it?</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'prometheusConfig' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration; factoryMethodName=prometheusConfig; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]] for bean 'prometheusConfig': There is already [Generic bean: class [kz.btsd.messenger.calls.config.PrometheusConfig]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/home/nurbol/BTSDigital/mess-calls/mess-calls-app/build/classes/kotlin/main/kz/btsd/messenger/calls/config/PrometheusConfig.class]] bound.
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:893)
      at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:274)
      at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:141)
      at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117)
      at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327)
      at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
      at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
      at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:119)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
      ... 73 more
  </code></pre>