alibaba / druid

阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
https://github.com/alibaba/druid/wiki
Apache License 2.0
27.9k stars 8.57k forks source link

druid-starter与p6spy集成的问题... #3190

Open Centerful opened 5 years ago

Centerful commented 5 years ago

之前基于xml的配置(spring4.x版本):

<!--数据库连接配置 -->
<bean id="publicDataSourceReal" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
...
</bean>

<!-- SQL数据源代理-监控 -->
<bean id="publicDataSource" class="com.p6spy.engine.spy.P6DataSource">
      <constructor-arg ref="publicDataSourceReal"></constructor-arg>
</bean>

后来将项目转为springboot项目后(springboot 2.1.x版本),使用druid-spring-boot-starter(使用的是1.1.9版本) 自己写了一个p6spy的起步依赖,想要集成druid的起步依赖,我想在自已的代码中创建druidDataSource对象(不注入Ioc容器中,那样做会出现循环引用的异常Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'getP6DataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?

集成代码如下:

@Configuration
@ConditionalOnClass(DruidDataSource.class)
@AutoConfigureBefore(DruidDataSourceAutoConfigure.class)
@EnableConfigurationProperties(P6SpyProperties.class)
public class P6SpyAutoConfiguration{
...
@Bean
@Primary
public P6DataSource p6DataSource () throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        P6DataSource p6 = new P6DataSource(**new DruidDataSourceWrapper()**);
        p6.setRealDataSource(p6SpyProperties.getRealDriver());
        P6Factory p6Factory = (P6Factory)Class.forName(p6SpyProperties.getModuleList()).newInstance();
        p6.setJdbcEventListenerFactory(p6Factory::getJdbcEventListener);
        return p6;
}
}

然后发现DruidDataSourceWrapper类是私有的无法使用,如果我将该类复制到本地,在打jar包引用后发现yml中对于druid的配置提供了两份ಠ_ಠ

有一种不怎么友好的解决方法,用硬编码的方式自己new一个druidDataSource对象并set值,然后放入p6spy中,这样便无法使用druid的起步依赖

还有一种方式是将整个druid起步依赖复制到p6spy起步依赖中:(。(或许可行)。

希望能够将DruidDataSourceWrapper类修改为public ^_^

不知道有没有更好的解决方法,thanks!

leafseelight commented 3 years ago

有druid为什么还要用p6spy呢?