JavaMelody 与 Spring Boot 和 Hikari 数据源

标签 java spring-boot java-melody

我正在 springboot projet 中安装 javamelody (根据本教程: https://github.com/javamelody/javamelody/wiki/SpringBootStarter :没有 spring-boot-starter 的安装),但是在运行服务器时。

在 application.yml 文件中,我添加 面向对象: 代理目标类:true 但仍然无法正常工作。

请帮忙。 我收到此错误:

"2017-07-07 14:55:20.582 WARN 10056 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ryantenney.metrics.spring.config.annotation.DelegatingMetricsConfiguration': Unsatisfied dependency expressed through method 'setMetricsConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'metricsConfiguration': Unsatisfied dependency expressed through field 'hikariDataSource'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dataSource' is expected to be of type 'com.zaxxer.hikari.HikariDataSource' but was actually of type 'com.sun.proxy.$Proxy132'
2017-07-07 14:55:20.585 ERROR 10056 --- [ restartedMain] o.s.b.f.s.DefaultListableBeanFactory : Destroy method on bean with name 'dataSourceInitializer' threw an exception

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7d8e76e9: startup date [Fri Jul 07 14:55:14 CEST 2017]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404)
at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97)
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:954)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:961)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:555)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at net.viamedis.kalivia.osteo.KaliviaOsteoApp.main(KaliviaOsteoApp.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

APPLICATION FAILED TO START

Description:

The bean 'dataSource' could not be injected as a 'com.zaxxer.hikari.HikariDataSource' because it is a JDK dynamic proxy that implements:
javax.sql.DataSource
java.io.Closeable

Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @enableasync and/or @enablecaching."

最佳答案

我也遇到了同样的问题。对我有用的是手动定义 HikariDataSource bean,因此您不需要设置 proxy-target-class=true:

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public HikariDataSource dataSource() {
    return (HikariDataSource) DataSourceBuilder.create().type(HikariDataSource.class).build();
}

然后将 application.yml 中的属性 url 更改为 jdbc-url。 我还为 javaMelody 添加了以下配置:

javamelody:
  enabled: true
  spring-monitoring-enabled: true
  init-parameters:
    log: true
    authorized-users: admin:********

现在,javaMelody 正在运行,并且 Spring Boot 应用程序可以正确启动。 依赖项是 "net.bull.javamelody:javamelody-spring-boot-starter:1.68.1"

提示:如果您的单元测试在添加 javaMelody 后被破坏,只需通过应用程序配置文件完全禁用它即可进行单元测试。

关于JavaMelody 与 Spring Boot 和 Hikari 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44974139/

相关文章:

grails - Grails Javamelody插件警告

JavaMelody 1.68.0 - JBoss 找不到 iText 类

javamelody tomcat虚拟主机

java - 强制调用专用方法

java - 在 Android 中读取/写入 SD 卡的最快方法?

java - 使用 Spring Data Cassandra 动态创建键空间、表和生成表

spring-boot - 何时配置 zuul 路由

Java JSF 关于自定义验证

java - 如何对我的顺序 Java 代码进行多线程处理

java - spring boot 依赖注入(inject)报错