Spring @Required Annotation

In a production-scale application, there may be hundreds or thousands of beans declared in the IoC container, and the dependencies between them are often very complicated. One of the shortcomings of setter injection is that it’s very hard for you to check if all required properties have been set or not. Using “dependency-check” attribute of <bean> you can check if attributes values have been set but it can’t check if their value is set to null or non-null values.

Apart from verifying dependencies using dependency-check, you can use @Required annotation to check if values set are non-null.

How to use @Required annotation

1) Use @Required annotation over setter methods

Use the @Required annotation over setter method of bean property in class file as below:

public class EmployeeFactoryBean extends AbstractFactoryBean<Object> 
{
	private String designation;
	
	public String getDesignation() {
		return designation;
	}

	@Required
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	
	//more code here
}

2) Register RequiredAnnotationBeanPostProcessor class

RequiredAnnotationBeanPostProcessor is a spring bean post processor that checks if all the bean properties with the @Required annotation have been set. To enable this bean post processor for property checking, you must register it in the Spring IoC container.

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

Demo

If any property with @Required have not been set, a BeanInitializationException will be thrown by this bean post processor. For example, if I will create an instance of EmployeeFactoryBean class without passing property value for designation, then I will get this error.

<bean id="manager"  class="com.howtodoinjava.demo.factory.EmployeeFactoryBean">
	<!-- <property name="designation" value="Manager" /> -->
</bean>

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

It will throw error.

Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'designation' is required for bean 'manager'
	at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	... 11 more

To correct this problem, pass the designation value by un-commenting the line in applicationContext.xml file.

In this way, you can use @Required annotation and RequiredAnnotationBeanPostProcessor class to verify that on context initialization, all the required bean properties have been set properly.

Happy Learning !!

4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Comments are closed for this article!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.