Annotation based Dependency Injection in Spring 3 and Hibernate 3 Framework

Spring is a popular framework for developing java-based applications. Annotations are easy to use and they make the code more readable, being less xml based. This article provides a step by step approach on how to implement annotation based dependency injection using the Spring3 and Hibernate 3 open source java frameworks.

Author: Preeti Shenoy


Spring is a popular framework for developing java-based applications. In initial versions of Spring, all the features like dependency injection (DI) were archived based on the xml configuration files. As the framework is evolving to version 3.x, the xml based configuration is enhanced by annotations based configuration. Annotations are easy to use, added in code at class, method or variable level. This makes code more readable, less xml based. In this article we will see how to use annotations based configuration in Spring 3, along with integration with Hibernate3 for data access layer.

Now let’s consider an  example which we will implement using Spring3 and Hibernate 3 and see how to implement it step by step.

Problem Statement

We will use as an example the creation a user bank account. In the first screen, the user is asked to fill up the account creation form. When this form is submitted, the user account is created in the database. If the creation of account is successful, a success message is displayed.


Typically a web application consists of three layers:Presentation, Business and Persistence. In this example, the mapping between layer and framework is as follows
1. Presentation Layer: Struts 2.0 framework
2. Business Layer:  Spring framework 3.0
3. Persistence Layer: Hibernate framework 3.0

Annotation based Dependency Injection in Spring 3 and Hibernate 3 Framework

To integrate the three layers, we will be using components below provided by different frameworks.

Context Loader Listener: This is used to integrate the Spring framework with other web frameworks like Struts. The declaration of this listener is done in web.xml.

Local Session Factory Bean: This is used to integrate Spring with Hibernate. It creates a local Hibernate SessionFactory instance, which can be used as a bean reference by the application that needs it. This declaration is done is the Spring configuration file.

Application Structure: The diagram below explains the design of the example we are considering for this demonstration.

welcome file

action=”createaccount” (struts.xml)


saveOrUpdate(bankAccount) /   List<BankAccount>
fetchAllBankAccounts() /  List

Project files


This is the deployment descriptor that is the entry point of our application and is present in WEB-INF directory. In this file, we configure StrutsPrepareAndExecuteFilter as the filter that enables the implementation of the Struts 2.0 framework in our demo. We also declare ContextLoaderListener as the listener, which is the plugin provided by the spring framework that facilitates struts-spring integration.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">


This is the jsp page where we enter the user details and forward them to the database for the account creation. When the user clicks on the “Submit” button, the Struts2 filter invokes the action class that is defined in the ‘action’ attribute of ‘s:form’ tag.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<%@taglib prefix="s" uri="/struts-tags" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Page</title>
<center><h1>ABC Bank</h1></center>
<h2>Bank Registration</h2>
<s:form action="createaccount" >
<s:textfield name="custName" label="Customer Name"/>
<s:textarea name="custAddress" label="Customer Address" value="" cols="30" rows="5" />
<s:radio name="custGender" label="Gender" list="{'Male','Female'}" />
<s:select name="accountType" list="{'Current','Savings'}" label="Account Type" />
<s:submit name="Submit"/>


In this jsp page, we can see the list of all accounts created in this particular scope. Note that account id is the auto-generated primary key.

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html" import="java.util.*"%>
<title>Success Page</title>
<h2>Account created successfully !!!</h2>
<table border="1" >
<th>Account ID</th>
<th>Customer Name</th>
<s:iterator value="savedBankAccountList">
<td><s:property value="accountId" /></td>
<td><s:property value="custName" /></td>


This is the central configuration file which takes request from the client and forwards them to controller which in turn invokes the action class capable of handling client request. It also facilitates view redirection based on the return value of execute method in action class.

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<package name="default" extends="struts-default">
<action name="createaccount">
<result name="successView">success.jsp</result>
<result name="input">RegisterUser.jsp</result>

This is the model class that is used to populate the bean object based on the parameters in the request object. In this demo, we have used a model-driven approach that requires us to create separate classes for model and action.

package com.abcbank.model;
public class BankAccount
private int accountId;
private String custName;
private String custAddress;
private String custGender;
private String accountType;
public int getAccountId() {
return accountId;
public void setAccountId(int accountId) {
this.accountId = accountId;
public String getCustName() {
return custName;
public void setCustName(String custName) {
this.custName = custName;
public String getCustAddress() {
return custAddress;
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
public String getCustGender() {
return custGender;
public void setCustGender(String custGender) {
this.custGender = custGender;
public String getAccountType() {
return accountType;
public void setAccountType(String accountType) {
this.accountType = accountType;

This is the action class that runs the business logic on the Model component of the application and modifies the contents of the database if necessary. Once the action is performed and the result is obtained, the control is returned back to the controller.

Since we are using a model driven approach, we need to override the getModel() method of ModelDriven and return an instance of model class.

package com.abcbank.action;
import com.abcbank.model.BankAccount;
import com.abcbank.dao.BankAccountDAO;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class CreateAccountAction extends ActionSupport implements ModelDriven<BankAccount>
private BankAccount bankAccount=new BankAccount();
private BankAccountDAO bankAccountDAO;
List<BankAccount> savedBankAccountList = new ArrayList<BankAccount>() ;
public BankAccount getModel()
return bankAccount;
public String execute() throws Exception
return "successView";
public BankAccount getBankAccount() {
return bankAccount;
public void setBankAccount(BankAccount bankAccount) {
this.bankAccount = bankAccount;
public void setBankAccountDAO(BankAccountDAO bankAccountDAO) {
this.bankAccountDAO = bankAccountDAO;
public List<BankAccount> getSavedBankAccountList() {
return savedBankAccountList;
public void setSavedBankAccountList(List<BankAccount> savedBankAccountList) {
this.savedBankAccountList = savedBankAccountList;

public interface IBankAccount {
public void saveOrUpdate(BankAccount bankAccount) ;
public List<BankAccount> fetchAllBankAccounts() ;

This class is a spring framework component that enables the usage of Hibernate framework in our demo. In this class we can access the persistence resource which is SessionFactory in the case of Hibernate.

package com.abcbank.dao;
import com.abcbank.model.BankAccount;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class BankAccountDAOImpl implements IBankAccount
private SessionFactory sessionFactory ;
public void setSessionFactory(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
public void saveOrUpdate(BankAccount bankAccount)
Session session = sessionFactory.openSession();
public List<BankAccount> fetchAllBankAccounts()
Session session = sessionFactory.openSession();
Query query = session.createQuery("from BankAccount");
List<BankAccount> list = (List<BankAccount>)query.list();
return list;


This is the mapping file which has the details about the object that needs to be persisted to the database.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.abcbank.model.BankAccount" table="BankAccount">
<id name="accountId" column="ACC_ID">
<property name="custName"><column name="CUST_NAME"/></property>
<property name="custAddress"><column name="CUST_ADDRESS"/></property>
<property name="custGender"><column name="CUST_GENDER"/></property>
<property name="accountType"><column name="ACC_TYPE"/></property>


This is a spring configuration file which has details of the database that our business logic should access. All the user defined files (classes,xml..) are to be declared as beans here.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<bean id="myDataSource" destroy-method="close">
<!-- <bean id="myDataSource" destroy-method="close"> -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@xxxxxxx:xxxx:xxxxx" ></property>
<property name="username" value=" "></property>
<property name="password" value=" "></property>
<bean id="sessionFactory">
<!-- <bean id="sessionFactory"> -->
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="">create</prop>
<bean id="bankAccountDAOBean" >
<property name="sessionFactory" ref="sessionFactory" />
<bean id="bankAccountAction" >
<property name="bankAccountDAO" ref="bankAccountDAOBean" />


To see the application running on the server, use the Eclipse IDE: right-click on the project folder in the project explorer tab / Select   Run As ” Run On Server”. The screen below will be displayed in the browser.

Annotation based Dependency Injection in Spring 3 and Hibernate 3 Framework

A) Now provide the user details and click the “Submit” button.

Annotation based Dependency Injection in Spring 3 and Hibernate 3 Framework

B) After the account creation, the application fetches the account details from database and displays them.

Annotation based Dependency Injection in Spring 3 and Hibernate 3 Framework

Pros/Cons of using Annotations based DI

1. The code needs to be recompiled in case there are changes in dependency. In case of XML configurations, the recompilation of code is not required.
2. Annotations based dependency injection is performed before XML injection. So if there are both types of DI used in the code, xml DI will override the previous one.
3. As DI becomes decentralized, if the annotations are used, it becomes harder to trace the dependencies between classes. While in case of XML configurations, the dependencies can be centrally tracked by analyzing the configuration files.
4. When annotations are used in the code, the code provides lot of context and wiring becomes more easy to understand [1].


1. Spring documentation:

About the author

Preeti Shenoy is a Technology Architect working in Infosys. She has more than 10 years of experience in implementing small and large web, portal and middleware projects in Java-J2EE space. She is an avid writer and has written many technical articles and papers. Linkedin profile:

One Comment

  1. Pingback: Software Development Linkopedia June 2013

Comments are closed.