HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Core / Spring @Required Annotation

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Shantanu

    May 12, 2019

    @Required is Deprecated

  2. Prabhat

    January 5, 2017

    @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

  3. Charith

    March 4, 2016

    Lokesh Gupta
    I used ” instead of

    Its getting same output now.Is it ok if I’m using ” ?
    Thanks

    • Lokesh Gupta

      March 4, 2016

      Please post the code inside [java] … [/java] tags.

Comments are closed on this article!

Search Tutorials

Spring Tutorial

  • Spring – Introduction
  • Spring – IoC Containers
  • Spring – IoC vs. DI
  • Spring – Bean Scopes
  • Spring – Bean Life Cycle
  • Spring – Bean Postprocessors
  • Spring – Autowiring
  • Spring – Annotations
  • Spring – Stereotype Annotations
  • Spring – Task Scheduling
  • Spring – Timer Task
  • Spring – Events
  • Spring – Message Source
  • Spring – ResourceLoader
  • Spring – Property Editor
  • Spring – Send Email
  • Spring – Version-less Schema
  • Spring – Interview Questions
  • Spring – Best Practices

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces