Checking Properties with the @Required Annotation

Spring’s dependency checking feature can only check for all properties of certain types. It’s not flexible enough to check for particular properties only. In most cases, you would like to check if particular properties have been set, but not all properties of certain types.

RequiredAnnotationBeanPostProcessor is a Spring bean post processor that checks if all the bean properties with the @Required annotation have been set. A bean post processor is a special kind of Spring bean that is able to perform additional tasks on each bean before its initialization. To enable this bean post processor for property checking, you must register it in the Spring IoC container. Note that this processor can only check if the properties have been set, but can’t check if their value is not null.

package com.sequence;
import org.springframework.beans.factory.annotation.Required;
public class SequenceGenerator {
private PrefixGenerator prefixGenerator;
private String suffix;
@Required
public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}
@Required
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}

To ask Spring to check if these properties have been set on all sequence generator instances, you have to register a RequiredAnnotationBeanPostProcessor instance in the IoC container. If you are using a bean factory, you have to register this bean post processor through the API. Otherwise, you can just declare an instance of this bean post processor in your application context.

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

If you are using Spring 2.5, you can simply include the <context:annotation-config> element in your bean configuration file, and a RequiredAnnotationBeanPostProcessor instance will automatically get registered.

If you are using Spring 3.0, you need only to add the @Required Annotation to the setter. Upon adding the annotation, you will need to add the import for org.springframework.beans.factory.annotation.Required.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
</beans>

If any properties with @Required have not been set, a BeanInitializationException will be thrown by this bean post processor.

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sequenceGenerator' defined in class path resource
[beans.xml]: Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanInitializationException: Property
'prefixGenerator' is required for bean 'sequenceGenerator'

This is the preffered way to require a setter to be validated. No need to remember to do it in your bean.