后来将项目转为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;
}
}
之前基于xml的配置(spring4.x版本):
后来将项目转为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?
集成代码如下:
然后发现DruidDataSourceWrapper类是私有的无法使用,如果我将该类复制到本地,在打jar包引用后发现yml中对于druid的配置提供了两份ಠ_ಠ
有一种不怎么友好的解决方法,用硬编码的方式自己new一个druidDataSource对象并set值,然后放入p6spy中,这样便无法使用druid的起步依赖。
还有一种方式是将整个druid起步依赖复制到p6spy起步依赖中:(。(或许可行)。
希望能够将DruidDataSourceWrapper类修改为public ^_^
不知道有没有更好的解决方法,thanks!