Spring @Required Annotation


 

Spring @Required Annotation

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

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.

Student.java
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) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

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

}

College.java

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

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

	@Required
	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;
	}
	
	@Override
	public String toString() {
		return "College [registration=" + registration + ", Student=" + student + 
                          ",year=" + year + "]";
	}
}

RequiredMain.java

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

context.xml

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

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

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

<!-- End -->

</beans>

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

Ads