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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Spring @Required Annotation”

  1. @Configuration
    public class BeanDefination {
    @Bean
    public Student student()
    {
    Student s= new Student();
    //s.setName(“Shiva”);
    return s;
    }

    }

    public class Student {
    public String name;
    public String getName() {
    return name;
    }
    @Required
    public void setName(String name) {
    this.name = name;

    }

    }

    public class TestDemo {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    AnnotationConfigApplicationContext factory=new AnnotationConfigApplicationContext();
    factory.register(BeanDefination.class);
    factory.refresh();
    Student student=(Student)factory.getBean(Student.class);
    System.out.println(student.getName());
    }

    }

    My Doubt is i have mentioned @Reuired and if i am not injecting any values then it is not throwing an error

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.