Saturday, September 29, 2012

How to use @Required Annotation in Spring?


In most cases, you would like to check if particular properties have been set, but not all properties of certain types. For this Spring provides @Required annotation in order to check a particular property. It works in Java1.5 and above versions only.
Let's use @Required annotation in  application.AccountService.java

  1. package org.myjavaswtech.service;  
  2.   
  3. import org.myjavaswtech.dao.AccountDAO;  
  4. import org.springframework.beans.factory.annotation.Required;  
  5.   
  6. public class AccountService {  
  7.   
  8.  private AccountDAO accountDao;  
  9.   
  10.  @Required  
  11.  public void setAccountDao(AccountDAO accountDao) {  
  12.   this.accountDao = accountDao;  
  13.  }   
  14.  public void transferFund(String acNo, String benAcNo, double amount){  
  15.   System.out.println("Call to Account DAO");  
  16.   accountDao.transferFund(acNo, benAcNo, amount);  
  17.  }  
  18. }  

Inorder to work @Required Annotation, you need to register RequiredAnnotationBeanPostProcessor instance in the IoC container.

RequiredAnnotationBeanPostProcessor is a Spring bean post processor that checks if all the bean properties with the @Required annotation have been set.

beans.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.  xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.  <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
  7.    
  8.  <bean name="accountDao" class="org.myjavaswtech.dao.AccountDAO">  
  9.  </bean>  
  10.   
  11.  <bean name="accountService" class="org.myjavaswtech.service.AccountService" dependency-check="objects">  
  12.   <!-- <property name="accountDao" ref="accountDao" /> -->  
  13.  </bean>  
  14.   
  15.  <bean name="accountBean" class="org.myjavaswtech.beans.AccountBean" >  
  16.   <property name="senderAcNo" value="ac123456"/>  
  17.   <property name="beneficiaryAcNo" value="ac7890" />  
  18.   <property name="amount" value="10000"/>  
  19.   <property name="accountService" ref="accountService"/>  
  20.  </bean>  
  21.   
  22. </beans>  
Now Run the application: You'll get the following exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'accountDao' is required for bean 'accountService'.

Spring2.5 and Above : <context:annotation-config>
In Spring2.5 and above versions, <context:annotation-config> is used in IOC container instead of RequiredAnnotationBeanPostProcessor. If you use <context:annotation-config> element in your bean configuration file, and a RequiredAnnotationBeanPostProcessor instance will automatically get registered.

beans.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.  xmlns:context="http://www.springframework.org/schema/context"  
  5.  xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.  http://www.springframework.org/schema/context  
  8.  http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.    
  10.  <context:annotation-config/>  
  11.    
  12.  <!-- <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> -->  
  13.    
  14.  <bean name="accountDao" class="org.myjavaswtech.dao.AccountDAO">  
  15.  </bean>  
  16.   
  17.  <bean name="accountService" class="org.myjavaswtech.service.AccountService" dependency-check="objects">  
  18.   <!-- <property name="accountDao" ref="accountDao" /> -->  
  19.  </bean>  
  20.   
  21.  <bean name="accountBean" class="org.myjavaswtech.beans.AccountBean" >  
  22.   <property name="senderAcNo" value="ac123456"/>  
  23.   <property name="beneficiaryAcNo" value="ac7890" />  
  24.   <property name="amount" value="10000"/>  
  25.   <property name="accountService" ref="accountService"/>  
  26.  </bean>  
  27.   
  28. </beans>  
In the above configuration we used context namespace and schema location.

Now Run the application: You'll get the following exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in class path resource [beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'accountDao' is required for bean 'accountService'.

1 comment:

  1. Woah! i am very fond the template/theme of this web log. It's straightforward, nonetheless effective. lots of times it's totally laborious to induce that "perfect balance" between excellent usability and visual look. i have to say you have got done a glorious job with this. in addition, the web log masses super fast on behalf of me on Opera. Exceptional Blog! additionally, please visit my web site.
    usb drive recovery

    ReplyDelete