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