Sunday, September 30, 2012

How to download and Install Hibernate framework?


Any framework software is not an installation software, i mean not an executable software.
A framework is nothing but a collection of class files bundled in the form of JAR files.

Hibernate framework is a set of JAR files. Working with hibernate means, just adding those hibernate jar files to the application.

Download Hibernate framework software from the URL http://sourceforge.net/projects/hibernate/files/hibernate3/
  • Choose hibernate version 3.2.2-ga.zip ,which is a standard version.
  • After downloaded, Unzip it.
  • Find Hibernate API document in the /doc/api folder.
  • In lib directory, you'll find various jar files.

Required JAR files:
  • antlr-2.7.6.jar
  • asm.jar
  • asm-attrs.jar
  • cglib-2.1.3.jar
  • commons-collections-2.1.1.jar
  • commons-logging-1.0.4.jar
  • ehcash.jar
  • dom4j-1.6.1.jar
  • hibernate3.jar
  • jta.jar
  • log4j-1.2.3.jar

    NOTE: For annotations some other jar files need to be added. When time comes i'll tell those jar files.

    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'.