Spring depends on attribute example


If a bean has a dependency of another bean and you want to initialize the dependent bean first, the depends-on attribute can explicitly force one or more beans to be initialized before the bean using this element is initialized. Typically to resolve this we uses the <ref/> element in XML-based configuration.

In this example there are three classes Principal, Teacher and Student. Here Principal depends on Teacher and Teacher depend on Student.

So the IoC container will first create the Student class object then Teacher class object and finally it will create the Principal class object.

<bean id="p" class="org.websparrow.beans.Principal" depends-on="t" />
<bean id="t" class="org.websparrow.beans.Teacher" depends-on="s" />
<bean id="s" class="org.websparrow.beans.Student" />

If the Principal class has a dependency on multiple beans, pass a list of bean names as the value of the depends-on attribute, with commas, whitespace, and semicolons, used as valid delimiters:

<bean id="p" class="org.websparrow.beans.Principal" depends-on="t,l" />
<bean id="t" class="org.websparrow.beans.Teacher" depends-on="s" />
<bean id="l" class="org.websparrow.beans.Librarian" />
<bean id="s" class="org.websparrow.beans.Student" />

Note: Mutual dependency are not possible. For example, if you pass the Principal class reference to the Student class, it will throw the exceptions.

Let’s check the complete example.

Spring Beans

Create the bean classes and invoke the default constructor and print some message.

Principal.java
package org.websparrow.beans;

public class Principal {

	public Principal() {
		System.out.println("Principal class object created.");
	}
}
Teacher.java
package org.websparrow.beans;

public class Teacher {

	Teacher() {
		System.out.println("Teacher class object created.");
	}
}
Student.java
package org.websparrow.beans;

public class Student {

	Student() {
		System.out.println("Student class object created.");
	}
}

Spring Beans Configuration

Configure all bean classes that have a dependency on another bean using depends-on attribute of <bean/> tag.

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="p" class="org.websparrow.beans.Principal" depends-on="t" />

	<bean id="t" class="org.websparrow.beans.Teacher" depends-on="s" />

	<bean id="s" class="org.websparrow.beans.Student" />

</beans>

Run it

Load the configuration file using J2EE container and run it.

Admin.java
package org.websparrow.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Admin {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
	}
}
Output:

You will get the following output on the console log.

Student class object created.
Teacher class object created.
Principal class object created.

Download Source Code: spring-depends-on-attribute-example


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.