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- package org.myjavaswtech.service;
- import org.myjavaswtech.dao.AccountDAO;
- import org.springframework.beans.factory.annotation.Required;
- public class AccountService {
- private AccountDAO accountDao;
- @Required
- public void setAccountDao(AccountDAO accountDao) {
- this.accountDao = accountDao;
- }
- public void transferFund(String acNo, String benAcNo, double amount){
- System.out.println("Call to Account DAO");
- accountDao.transferFund(acNo, benAcNo, amount);
- }
- }
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
- <?xml version="1.0" encoding="UTF-8"?>
- <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 name="accountDao" class="org.myjavaswtech.dao.AccountDAO">
- </bean>
- <bean name="accountService" class="org.myjavaswtech.service.AccountService" dependency-check="objects">
- <!-- <property name="accountDao" ref="accountDao" /> -->
- </bean>
- <bean name="accountBean" class="org.myjavaswtech.beans.AccountBean" >
- <property name="senderAcNo" value="ac123456"/>
- <property name="beneficiaryAcNo" value="ac7890" />
- <property name="amount" value="10000"/>
- <property name="accountService" ref="accountService"/>
- </bean>
- </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
- <?xml version="1.0" encoding="UTF-8"?>
- <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 class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> -->
- <bean name="accountDao" class="org.myjavaswtech.dao.AccountDAO">
- </bean>
- <bean name="accountService" class="org.myjavaswtech.service.AccountService" dependency-check="objects">
- <!-- <property name="accountDao" ref="accountDao" /> -->
- </bean>
- <bean name="accountBean" class="org.myjavaswtech.beans.AccountBean" >
- <property name="senderAcNo" value="ac123456"/>
- <property name="beneficiaryAcNo" value="ac7890" />
- <property name="amount" value="10000"/>
- <property name="accountService" ref="accountService"/>
- </bean>
- </beans>
In the above configuration we used context namespace and schema location.
Namespace: xmlns:context="http://www.springframework.org/schema/context
SchemaLocation: http://www.springframework.org/schema/context
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'.
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.
ReplyDeleteusb drive recovery