Checking for dependencies

The Spring IoC container also has the ability to check for the existence of unresolved dependencies of a bean deployed into the container. This feature is sometimes useful when you want to ensure that all properties (or all properties of a certain type) are set on a bean.

Table 1.1 Dependency checking modes
Mode Explanation
none No dependency checking. Properties of the bean which have no value specified for them are simply not set.
simple Dependency checking is performed for primitive types and collections (everything except collaborators).
object Dependency checking is performed for collaborators only.
all Dependency checking is done for collaborators, primitive types and collections.


No Dependency Checking

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	</bean>

	<bean id="address" class="com.kruders.bean.Address">
		<property name="city" value="Delhi" />
		<property name="country" value="India" />
	</bean>

Simple Dependency Checking

	<bean id="employee" class="com.kruders.bean.Employee" dependency-check="simple">
	   <property name="name" value="Puneet" />
	</bean>

	<bean id="address" class="com.kruders.bean.Address">
		<property name="city" value="Delhi" />
		<property name="country" value="India" />
	</bean>

If you do not specify any primitive or collection type, you will get UnsatisfiedDependencyException like:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee' defined in class path resource [Spring-Beans.xml]: Unsatisfied dependency expressed through bean property 'name': Set this property value or disable dependency checking for this bean.

Objects Dependency Checking

	<bean id="employee2" class="com.kruders.bean.Employee" dependency-check="objects">
	   <property name="name" value="Puneet" />
	   <property name="address" ref="address" />
	</bean>

	<bean id="address" class="com.kruders.bean.Address">
		<property name="city" value="Delhi" />
		<property name="country" value="India" />
	</bean>

If you do not specify object type, you will get UnsatisfiedDependencyException like:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee2' defined in class path resource [Spring-Beans.xml]: Unsatisfied dependency expressed through bean property 'address': Set this property value or disable dependency checking for this bean.

All Dependency Checking

	<bean id="employee3" class="com.kruders.bean.Employee" dependency-check="all">
	   <property name="name" value="Puneet" />
	   <property name="address" ref="address" />
	</bean>
	
	<bean id="address" class="com.kruders.bean.Address">
		<property name="city" value="Delhi" />
		<property name="country" value="India" />
	</bean>

If you do not specify any of primitive, collection or object type, you will get UnsatisfiedDependencyException like:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employee3' defined in class path resource [Spring-Beans.xml]: Unsatisfied dependency expressed through bean property 'name': Set this property value or disable dependency checking for this bean.


You can download the source code of this example here.


One Response to Checking for dependencies

  1. rajiv September 11, 2014 at 7:47 am #

    hey

    if i write different setter method which don’t have any variable name in class of that type but it execute successfully why?

    here property name of lone is setter but it has no variable only setter is available

    package com.ci.beans;

    public class Loan {
    public String loanviewinfo()
    {
    System.out.println(“lone is 2% of 2000″);
    return “2000″;

    }
    public Loan() {
    // TODO Auto-generated constructor stub
    System.out.println(“loan class created”);
    }

    }

    package com.ci.beans;

    public class Person {

    private Loan obj;
    public void showviewloan()
    {
    obj.loanviewinfo();
    }

    public void setObj(Loan obj) {
    System.out.println(“set the obj “);
    this.obj = obj;
    }
    public void setLone(Loan obj) {
    System.out.println(“set the lone”);
    this.obj = obj;
    }
    public Person() {
    // TODO Auto-generated constructor stub
    System.out.println(“person class created”+obj);
    }
    }

    package com.ci.test;

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

    import com.ci.beans.Person;

    public class Test {
    public static void main(String[] args) {
    BeanFactory factory =new XmlBeanFactory(new ClassPathResource(“com/ci/commom/application-context.xml”));

    Person po=factory.getBean(“person”,Person.class);
    po.showviewloan();

    }
    }

Leave a Reply