Spring dependency checking with @Required Annotation

Spring’s dependency checking in bean configuration file is used to make sure all properties of a certain types (primitive, collection or object) have been set. In most scenarios, you just need to make sure a particular property has been set, but not all properties..

For this case, you need @Required annotation, see following example :

@Required example

A Customer object, apply @Required in setPerson() method to make sure the person property has been set.


package com.mkyong.common;

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

public class Customer 
{
	private Person person;
	private int type;
	private String action;
	
	public Person getPerson() {
		return person;
	}
	@Required
	public void setPerson(Person person) {
		this.person = person;
	}
}

Simply apply the @Required annotation will not enforce the property checking, you also need to register an RequiredAnnotationBeanPostProcessor to aware of the @Required annotation in bean configuration file.

The RequiredAnnotationBeanPostProcessor can be enabled in two ways.

1. Include <context:annotation-config />

Add Spring context and <context:annotation-config /> in bean configuration file.


<beans 
	...
	xmlns:context="http://www.springframework.org/schema/context"
	...
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	...
	<context:annotation-config />
	...
</beans>

Full example,


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

	<context:annotation-config />

	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>
2. Include RequiredAnnotationBeanPostProcessor

Include ‘RequiredAnnotationBeanPostProcessor’ directly in bean configuration file.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean 
class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
	
	<bean id="CustomerBean" class="com.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>

If you run it , the following error message will be throw, because person property is unset.


org.springframework.beans.factory.BeanInitializationException: 
	Property 'person' is required for bean 'CustomerBean'

Conclusion

Try @Required annotation, it is more flexible than dependency checking in XML file, because it can apply to a particular property only.

Custom @Required
Please read this article about how to create a new custom @Required-style annotation.

Reference

  1. http://static.springsource.org/spring/docs/2.5.x/reference/metadata.html#metadata-annotations-required

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Prabhat
Guest
Prabhat

Why @Required Annotation using Java Configuration doesn’t throw an error if required value is not set.
public class Student {
public String name;

public String getName() {
return name;
}

@Required
public void setName(String name) {
this.name = name;

}

}

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

}

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());
}

}

Mike
Guest
Mike

testing the comments… just for fun…

Sarneet Sethi
Guest
Sarneet Sethi

This is exactly what I was looking for. Thanks!

inei.me
Guest
inei.me

Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that
over again. Anyway, just wanted to say great blog!

duo
Guest
duo

How do you do this without using xml?

trackback
Spring properties dependency checking

[…] types (primitive, collection or object). The @Required Annotation can enforce this checking, see detail. […]

bayilik
Guest
bayilik

We started to a program as Bayilik Franchise Program. This infos gave help us for develop our program. We will send our program for your ideas.

trackback
Spring – Define custom @Required-style annotation | Spring

[…] custom @Required-style annotation Written on March 18, 2010 at 7:43 am by mkyong The @Required annotation is used to make sure a particular property has been set. If you are migrate your existing project […]

trackback
Daily del.icio.us for March 9th through March 13th — Vinny Carpenter's blog

[…] Spring – Dependency checking with @Required Annotation | Spring – The @Required Annotation is more flexible than dependency checking in XML file, because it can apply to a particular property only […]

letterheads
Guest
letterheads

Great post! I’m doing a lot of research on this at the moment and your blog is the best resource. Thanks again!