https://www.baeldung.com/circular-dependencies-in-spring
1. What Is a Circular Dependency?
It happens when a bean A depends on another bean B, and the bean B depends on the bean A as well:
Bean A → Bean B → Bean A
Of course, we could have more beans implied:
Bean A → Bean B → Bean C → Bean D → Bean E → Bean A
2. What Happens in Spring
When Spring context is loading all the beans, it tries to create beans in the order needed for them to work completely. For instance, if we didn’t have a circular dependency, like the following case:
Bean A → Bean B → Bean C
Spring will create bean C, then create bean B (and inject bean C into it), then create bean A (and inject bean B into it).
But, when having a circular dependency, Spring cannot decide which of the beans should be created first, since they depend on one another. In these cases, Spring will raise a BeanCurrentlyInCreationException while loading context.
It can happen in Spring when using constructor injection; if you use other types of injections you should not find this problem since the dependencies will be injected when they are needed and not on the context loading.
3. A Quick Example
Let’s define two beans that depend on one another (via constructor injection):
1
2
3
4
5
6
7
8
9
10
|
@Component
public
class
CircularDependencyA {
private
CircularDependencyB circB;
@Autowired
public
CircularDependencyA(CircularDependencyB circB) {
this
.circB = circB;
}
}
|
1
2
3
4
5
6
7
8
9
10
|
@Component
public
class
CircularDependencyB {
private
CircularDependencyA circA;
@Autowired
public
CircularDependencyB(CircularDependencyA circA) {
this
.circA = circA;
}
}
|
Now we can write a Configuration class for the tests, let’s call it TestConfig, that specifies the base package to scan for components. Let’s assume our beans are defined in package “com.baeldung.circulardependency”:
1
2
3
4
|
@Configuration
@ComponentScan
(basePackages = {
"com.baeldung.circulardependency"
})
public
class
TestConfig {
}
|
And finally we can write a JUnit test to check the circular dependency. The test can be empty, since the circular dependency will be detected during the context loading.
1
2
3
4
5
6
7
8
9
|
@RunWith
(SpringJUnit4ClassRunner.
class
)
@ContextConfiguration
(classes = { TestConfig.
class
})
public
class
CircularDependencyTest {
@Test
public
void
givenCircularDependency_whenConstructorInjection_thenItFails() {
// Empty test; we just want the context to load
}
}
|
If you try to run this test, you will get the following exception:
BeanCurrentlyInCreationException: Error creating bean with name
'circularDependencyA'
:
Requested bean is currently in creation: Is there an unresolvable circular reference?
|
4. The Workarounds
We will show some of the most popular ways to deal with this problem.
4.1. Redesign
When you have a circular dependency, it’s likely you have a design problem and the responsibilities are not well separated. You should try to redesign the components properly so their hierarchy is well designed and there is no need for circular dependencies.
If you cannot redesign the components (there can be many possible reasons for that: legacy code, code that has already been tested and cannot be modified, not enough time or resources for a complete redesign…), there are some workarounds to try.
4.2. Use @Lazy
A simple way to break the cycle is saying Spring to initialize one of the beans lazily. That is: instead of fully initializing the bean, it will create a proxy to inject it into the other bean. The injected bean will only be fully created when it’s first needed.
To try this with our code, you can change the CircularDependencyA to the following:
1
2
3
4
5
6
7
8
9
10
|
@Component
public
class
CircularDependencyA {
private
CircularDependencyB circB;
@Autowired
public
CircularDependencyA(
@Lazy
CircularDependencyB circB) {
this
.circB = circB;
}
}
|
If you run the test now, you will see that the error does not happen this time.