Spring Dependency-Check

In this tutorial you will learn how to check for dependency in spring framework.


Share on Google+Share on Google+

Spring Dependency-Check


In this tutorial you will learn how to check for dependency in spring framework.

Spring Dependency-Check

In this tutorial you will see how to check for unresolved dependencies of a bean that is deployed into the spring IoC container. It check for all bean's dependencies, that is expressed in its properties are satisfied or not. The dependency-check attribute is set in the <bean/> element. The dependency-check have four modes.

Mode Explanation
none It is default which tells no dependency checking is to be done. The properties of the bean that have no value specified will not be set.
simple Dependency checking for only primitive types and collections.
object Dependency checking for only collaborators only.
all Dependency checking for all primitive types, collections and collaborators.

Lets see an example of different mode of dependency-check.


package spring.dependency.check.mode;

public class StudentBean {
  private int rollNo;

  private StudentBean nestedStudentBean;

  public void setRollNo(int rollNo) {
    this.rollNo = rollNo;

  public void setNestedStudentBean(StudentBean nestedStudentBean) {
    this.nestedStudentBean = nestedStudentBean;

  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append(", nestedStudentBean=").append(nestedStudentBean);
    return sb.toString();


package spring.dependency.check.mode;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class DependecyCheckMain {
  public static void main(String[] args) {
    XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource(


<beans xmlns="http://www.springframework.org/schema/beans"

<!-- Spring Dependency-Check -->

<bean id="student1" class="spring.dependency.check.mode.StudentBean"
    <property name="rollNo" value="101" />

  <bean id="student2" class="spring.dependency.check.mode.StudentBean"
    <property name="nestedStudentBean" ref="nestedStudentBean" />

  <bean id="student3" class="spring.dependency.check.mode.StudentBean"
    <property name="nestedStudentBean" ref="nestedStudentBean" />
    <property name="rollNo" value="101" />

  <bean id="nestedStudentBean" 
="spring.dependency.check.mode.StudentBean" />

<!-- End  -->


When you run this application it will display output as shown below:

StudentBean{RollNo=101, nestedStudentBean=null}
StudentBean{RollNo=0, nestedStudentBean=StudentBean{RollNo=0, nestedStudentBean=null}}
StudentBean{RollNo=101, nestedStudentBean=StudentBean{RollNo=0, nestedStudentBean=null}}

Download this example code


Posted on: August 27, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles