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.
package org.websparrow.beans;
public class Principal {
public Principal() {
System.out.println("Principal class object created.");
}
}
package org.websparrow.beans;
public class Teacher {
Teacher() {
System.out.println("Teacher class object created.");
}
}
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.
<?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.
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");
}
}
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
References
Similar Posts
- Spring Boot Profiles and Configuration Management Example
- One to One mapping in JPA with Spring Boot
- Spring dependency check using @Required annotation example
- Display all beans name loaded by Spring Boot
- Spring Boot- Display image from database and classpath
- Spring @RestController, @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping Annotation Example
- Secondary type Dependency Injection in Spring
- Getting Started with Spring Boot and MongoDB
- Spring MVC @Controller, @RequestMapping, @RequestParam, and @PathVariable Annotation Example
- Spring Boot – Calling REST Services with RestTemplate