I am writing a bean that wraps a DataSource to add some internal functionality to it. It seems that MyDataSource is trying to use itself in the constructor. My new class is shown below:
@Component
public class MyDataSource implements DataSource {
public MyDataSource(DataSource wrapped) {
// do something...
}
}
When I try to run the application I get:
Error creating bean with name 'myDataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?
I don't control the creation of the normal DataSource at all, so it doesn't have a Qualifier or @Primary. I do control MyDataSource.
How do I tell my bean that it should use "the other DataSource" instead, and not itself during instantiation?
This is common when your custom bean and the original bean are of the same interface type, like
DataSourcein your case. To solve this issue, you need to guide Spring on which bean to inject where, ensuring that yourMyDataSourcedoes not attempt to wrap itself, causing a circular reference.There are several strategies to resolve this, but one common approach is to use
@Qualifierannotation or defining your bean with a specific method and using@Primary. However, as you mentioned that you do not control the creation of the originalDataSourceand it does not have a@Qualifieror@Primary, we need to find another way.A practical approach in your situation is to define an @Configuration class that explicitly configures your custom DataSource and uses it to wrap the existing DataSource bean.
@ComponentfromMyDataSource. This prevents Spring from trying to instantiate it automatically.MyDataSourceand inject the existingDataSource.This configuration explicitly declares that your
MyDataSourceshould wrap theDataSourcebean that Spring automatically configures. SinceMyDataSourceis no longer annotated with@Component, Spring will not try to instantiate it automatically, thus avoiding the circular reference.