Metrics - Error creating bean with name 'com.ryantenney.metrics.spring.reporter.Slf4jReporterFactory

在使用spring整合metrics时,启动web项目报错:

[WARN ] [2018-01-15 14:03:24] org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ryantenney.metrics.spring.reporter.Slf4jReporterFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com.codahale.metrics.Slf4jReporter$Builder.prefixedWith(Ljava/lang/String;)Lcom/codahale/metrics/Slf4jReporter$Builder;
 [ERROR] [2018-01-15 14:03:24] org.springframework.web.context.ContextLoader - Context initialization failed
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ryantenney.metrics.spring.reporter.Slf4jReporterFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com.codahale.metrics.Slf4jReporter$Builder.prefixedWith(Ljava/lang/String;)Lcom/codahale/metrics/Slf4jReporter$Builder;
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4812)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5255)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodError: com.codahale.metrics.Slf4jReporter$Builder.prefixedWith(Ljava/lang/String;)Lcom/codahale/metrics/Slf4jReporter$Builder;
	at com.ryantenney.metrics.spring.reporter.Slf4jReporterFactoryBean.createInstance(Slf4jReporterFactoryBean.java:56)
	at com.ryantenney.metrics.spring.reporter.Slf4jReporterFactoryBean.createInstance(Slf4jReporterFactoryBean.java:26)
	at com.ryantenney.metrics.spring.reporter.AbstractReporterFactoryBean.afterPropertiesSet(AbstractReporterFactoryBean.java:73)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
	... 21 more
根据异常的错误信息可以知道,是在com.codahale.metrics.Slf4jReporter$Builder类(内部类)中没有找到 prefixedWith(Ljava/lang/String;)方法。

原因是:使用spring注入Log4jReporter时,metrics-core jar包的版本过低,在该类中没有这个方法。

解决方法:升级metrics-core的版本即可。

开始,我的metrics-core jar包版本使用的是3.0.2,升级到3.2.5,在3.0.2版本中,该内部类中有prefixedWith()方法。

附:后续Spring集成Metrics的使用将会更新出来,博主在用到Metrics的时候,查找资料那叫一个头大啊!随后,我会将如何使用逐步更新出来,欢迎评论交流。

你可能感兴趣的:(Metrics)