Circular Dependency in Spring

This tutorial explains one of the basic mistake done by the spring developers while defining the spring beans. Spring will not allow Circular Dependency and will throw an exception (BeanCurrentlyInCreationException) at the time of loading the configuration files. Also it is not good practice to design your project to have the circular dependency. What is circular dependency?. For example : Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection. When you try to inject both the beans to each other. spring’s IOC container detects it as circular dependency. Lets look at the below code snippet.

ObjA.java

package javabeat.net.core.ioc;

public class ObjA {
	private ObjB b;
	public ObjA(ObjB b){
		this.b = b;
	}
}

ObjB.java

package javabeat.net.core.ioc;

public class ObjB {
	private ObjA a;
	public ObjB(ObjA a){
		this.a = a;
	}
}

Spring Beans Configuration

<context:component-scan base-package="javabeat.net.core.ioc"/>
	<bean id="aBean" class="javabeat.net.core.ioc.ObjA">
		<constructor-arg ref="bBean"/>
	</bean>
	<bean id="bBean" class="javabeat.net.core.ioc.ObjB">
		<constructor-arg ref="aBean"/>
	</bean>

SpringContextLoader.java

package javabeat.net.core.ioc;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContextLoader {
	public static void main (String args[]){
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("javabeat/net/core/ioc/spring-beans-ioc.xml");
		applicationContext.close();
	}
}

When you run the above example, you will get the following exception:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'aBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.
beforeSingletonCreation(DefaultSingletonBeanRegistry.java:327)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.
getSingleton(DefaultSingletonBeanRegistry.java:217)
	at org.springframework.beans.factory.support.AbstractBeanFactory.
doGetBean(AbstractBeanFactory.java:292)
	at org.springframework.beans.factory.support.AbstractBeanFactory.
getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:323)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!