DependsOn in Spring with Example

DependsOn in Spring with Example

The depends-on attribute can initialize one or more beans before the other bean.
we can configure depends-on in spring-xml file like following.
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />
Lets See example to DependsOn in Spring
Tools Used In This Article
  • Spring 4.3.4
  • Maven 4.0.0
  • JDK 1.6
First create Maven Project from the Ecclipse and below is Project Directory Structure
Project Structure

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<groupId>SpringDependsOnDemo</groupId>
<artifactId>SpringDependsOnDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.2.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Next Step is Writing Spring Beans
DemoBean1.java
package com.java2study;

public class DemoBean1 {
private DemoBean2 demoBean2;

public DemoBean1() {
System.out.println("DemoBean1 is initialized");

}

public DemoBean2 getDemoBean2() {
return demoBean2;
}

public void setDemoBean2(DemoBean2 demoBean2) {
this.demoBean2 = demoBean2;
}



}
DemoBean2.java
package com.java2study;

public class DemoBean2 {
private DemoBean3 demoBean3;

public DemoBean2() {
System.out.println("DemoBean2 is initialized");

}

public DemoBean3 getDemoBean3() {
return demoBean3;
}

public void setDemoBean3(DemoBean3 demoBean3) {
this.demoBean3 = demoBean3;
}

}
DemoBean3.java
package com.java2study;

public class DemoBean3 {
public DemoBean3() {
System.out.println("DemoBean3 is initialized");

}

}
applicationContext.xml
<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-4.3.xsd"
>
<bean id="demoBean1" class="com.java2study.DemoBean1" depends-on="demoBean2,demoBean3">
<property name="demoBean2" ref="demoBean2"></property>
</bean>

<bean id="demoBean2" class="com.java2study.DemoBean2">
<property name="demoBean3" ref="demoBean3"></property>
</bean>

<bean id="demoBean3" class="com.java2study.DemoBean3">

</bean>

</beans>
Now Create Test class and Run The Code
package com.java2study;

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

public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


}

}

output/p>;

DemoBean2 is initialized
DemoBean3 is initialized
DemoBean1 is initialized

Download SourceCode
-->