Spring Dependency-Check


 

Spring Dependency-Check

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

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.

StudentBean.java

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;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("StudentBean");
    sb.append("{RollNo=").append(rollNo);
    sb.append(", nestedStudentBean=").append(nestedStudentBean);
    sb.append('}');
    return sb.toString();
  }
}

DependecyCheckMain.java

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(
        "context.xml"));
    System.out.println(bf.getBean("student1"));
    System.out.println(bf.getBean("student2"));
    System.out.println(bf.getBean("student3"));
  }
}

context.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-2.5.xsd">

<!-- Spring Dependency-Check -->

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

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

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

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

<!-- End  -->

</beans>

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

Ads