Checking Properties with Dependency Checking

Spring’s dependency checking feature can help you to check if all properties of certain types have been set on a bean. You simply have to specify the dependency checking mode in the dependency-check attribute of <bean>. Note that the dependency checking feature can only check if the properties have been set, but can’t check if their value is not null. The Table below lists all the dependency checking modes supported by Spring.

Mode Description
none* No dependency checking will be performed. Any properties can be left unset.
simple If any properties of the simple types (the primitive and collection types) have not been set, an UnsatisfiedDependencyException will be thrown.
objects If any properties of the object types (other than the simple types) have not been set, an UnsatisfiedDependencyException will be thrown.
all If any properties of any type have not been set, an UnsatisfiedDependencyException will be thrown.

* The default mode is none, but this can be changed by setting the default-dependency-check attribute of the <beans> root element. This default mode will be overridden by a bean’s own mode if specified.You must set this attribute with great care as it will alter the default dependency checking mode for all the beans in the IoC container.

Checking Properties of the Simple Types

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator"
dependency-check="simple">
<property name="initial" value="100000" />
<property name="prefixGenerator" ref="datePrefixGenerator" />
</bean>

If any properties of such types have not been set, an UnsatisfiedDependencyException will be thrown, indicating the unset property.

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

Checking Properties of the Object Types

If the prefix generator is not set, then the evil NullPointerException will be thrown when prefix generation is requested. To enable dependency checking for bean properties of object types, (i.e., other than simple types), change the dependency-check attribute to objects.
<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator"
dependency-check="objects">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>

Then when you run the application, Spring will notify you that the prefixGenerator property has not been set.

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

Checking Properties of All Types

If you would like to check all bean properties whatever the type is, you can change the dependency-check attribute to all.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator"
dependency-check="all">
<property name="initial" value="100000" />
</bean>

Dependency Checking and Constructor Injection

Spring’s dependency checking feature will only check if a property has been injected via the setter method. So, even if you have injected the prefix generator via a constructor, an UnsatisfiedDependencyException will still be thrown.

<bean id="sequenceGenerator"
class="com.sequence.SequenceGenerator"
dependency-check="all">
<constructor-arg ref="datePrefixGenerator" />
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>

This is the more difficult way of doing it. It must be completed on the bean.

The preffered way is to use the Annotation of @Required in the class itself. More to come on this