Resolving Circular Dependency Issues in SpringBoot: Causes, Exceptions, and Practical Solutions

This article explains why SpringBoot throws BeanCurrentlyInCreationException when circular dependencies arise, especially with constructor injection, and provides multiple mitigation techniques such as disabling the restriction, using @Lazy, setter injection, and direct ApplicationContext bean retrieval.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Resolving Circular Dependency Issues in SpringBoot: Causes, Exceptions, and Practical Solutions

When Spring cannot resolve a circular dependency, it throws a BeanCurrentlyInCreationException indicating that a bean is currently in creation and an unresolvable circular reference exists.

Although Spring uses a three‑level cache to handle many circular dependencies, constructor‑based injection often leads to situations where the container cannot break the cycle, resulting in the above exception.

To address the problem you can:

Redesign the architecture to eliminate circular references.

Enable circular references in SpringBoot 2.6.x by setting the property:

spring:
  main:
    allow-circular-references: true

Apply @Lazy together with @Autowired for field injection:

@Lazy
@Autowired
private MyBean myBean;

Use @Lazy on constructor parameters:

@Component
public class MyBean {
    private MyBean2 myBean2;
    @Autowired
    public MyBean(@Lazy MyBean2 myBean2) {
        this.myBean2 = myBean2;
    }
}

Inject dependencies via setter methods:

@Autowired
public void setDao(MyDao dao) {
    this.dao = dao;
}

Obtain beans directly from the ApplicationContext when needed:

@Component
public class MyContextProvider {
    @Autowired
    private ApplicationContext appContext;
    public <T> T getBean(Class<T> beanClass) {
        return appContext.getBean(beanClass);
    }
    public Object getBean(String beanName) {
        return appContext.getBean(beanName);
    }
}

If redesign is not feasible, applying one of the above techniques can help you work around the circular dependency while keeping the application stable.

In summary, avoid circular dependencies whenever possible; if they cannot be avoided, use the provided configuration options, lazy injection, setter injection, or direct context lookup to mitigate the issue.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpringBootdependency-injectioncircular-dependency
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.