dependsOn attribute for loading the depended beans referenced by another bean. dependsOn is a bean tag that can indicate bean depends on another bean .
In this Example we can see when ApplicationContext Container is initialize first it will initialize Course Bean because we can put depends-on attribute ans give the dependency to the Student bean.
Course.java
package com.manish;
public class Course {
private String name;
Course()
{
System.out.println("Course Bean Creation : Initializing Course Details...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.java
package com.manish;
public class Student
{
private int id;
private String name;
Student()
{
System.out.println("Student Bean");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
App.java
package com.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.manish.Course;
import com.manish.Student;
public class App {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
}
}
app-context.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.xsd">
<bean id="student" class="com.manish.Student" depends-on="course">
<property name="id" value="1"></property>
<property name="name" value="manish"></property>
<property name="course" ref="course"></property>
</bean>
<bean id="course" class="com.manish.Course">
<property name="name" value="mca"></property>
</bean>
</beans>
Output:
Course Bean Creation : Initializing Course Details...
Student Bean
0 Comment(s)