Share on Google+Share on Google+

Spring @Required Annotation

In this tutorial you will see about spring @required annotation with an example.

Spring @required Annotation

In this tutorial you will see about spring @required annotation with an example. The @Required when written on top of setStudent() method it make sure that student property must have be set else it will give compile time error message that org.springframework.beans.factory.BeanInitializationException: Property 'student' is required for bean 'college' which is clearly shown in this example.

Simply apply the @Required annotation will not enforce the property checking, you have to register an RequiredAnnotationBeanPostProcessor to aware of the @Required annotation in bean configuration file. Simply applying @Required annotion is not enough to enforce the property checking, you also have to register RequiredAnnotationBeanPostProcessor in bean configuration file to do so. This configuration is done in two ways one by Include <context:annotation-config /> and another way is Include RequiredAnnotationBeanPostProcessor in bean configuration file. which is shown in this example's context.xml file.
public class Student {
	private String age;
	private String name;
	private String address;

	public String getAge() {
		return age;

	public void setAge(String age) {
		this.age = age;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getAddress() {
		return address;

	public void setAddress(String address) {
		this.address = address;
	public String toString() {
		return "Student [name=" + name + ", age=" + age + 
                          ",address=" + address + "]";


import org.springframework.beans.factory.annotation.Required;

public class College {
	private Student student;
	private String registration;
	private String year;

	public void setStudent(Student student) {
		this.student = student;

	public String getRegistration() {
		return registration;

	public void setRegistration(String registration) {
		this.registration = registration;

	public String getYear() {
		return year;

	public void setYear(String year) {
		this.year = year;
	public String toString() {
		return "College [registration=" + registration + ", Student=" + student + 
                          ",year=" + year + "]";

import org.springframework.beans.factory.BeanFactory;

public class RequiredMain {
	public static void main(String[] args) {
		BeanFactory beanfactory = new ClassPathXmlApplicationContext(
		College coll = (College) beanfactory.getBean("college");


<beans xmlns=""

<!-- one way -->
<context:annotation-config />

<!-- Another way  -->
<!-- <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> -->

<bean id="college" class="spring.required.annotation.College" >
    <property name="registration" value="BL001" />
    <property name="year" value="2001" />

<bean id="student" class="spring.required.annotation.Student">
  <property name="address" value="Delhi" />
  <property name="age" value="22" />
  <property name="name" value="Raj" />

<!-- End -->


When you run this application it will display compile time error message as shown below:

org.springframework.beans.factory.BeanInitializationException: Property 'student' is required for bean 'college'

Download this example code



Posted on: August 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null