Spring MVC Hibernate MySQL CRUD Operation

Spring MVC Hibernate MySQL CRUD Operation:
The idea of MVC framework is to separate the business logic from front end and change them without affecting others.

Model: Represents data
View: User Interface of the application
Controller: Receives request and perform the operations


  • The request is received by the dispatcher servlet
  • The dispatcher Servlet checks with the Handler Mappings to determine the appropriate controller to pass the request
  • The dispatcher Servlet get the appropriate controller from Handler Mapping and passes the request to Controller.
  • The controller sends the request along with the object containing the name of the view and data to be displayed, but not the exact logical name
  • Dispatcher Servlet after receiving the name, checks with view resolver to resolve the actual logical name.
  • Finally the view is displayed to the user

Now let us see the example of Spring MVC, Hibernate with MySQL.

Project Structure:














On clicking Edit


Create Another record,

Deleting Alpha record,





Read More

Life Cycle of Bean Factory and Application Context

Life Cycle of Bean Factory and Application Context

Life Cycle of Bean Factory
In Spring, Bean factory is used to instantiate, configure and manage objects. The objects created by Bean factory are aware of the relationships with other objects. Bean factory also handles the life cycle of objects.

Bean factory container instantiates bean from XML configuration file.

Populate Properties:
Bean factory populates all the properties from XML file.

Set Bean Name:
Name of the bean is set, This is done by passing the bean id in XML file to setBeanName() method (from BeanNameAware Interface)

Set Bean Factory:
In this step, Bean is given a reference to the bean factory. This is done by method setBeanFactory() from BeanFactoryAware interface.

This performs all the tasks that needs to be performed before the been is initialized. This is done by postProcessBeforeInitialization() method from BeanPostProcessor interface.

Initialize Beans:
Performs initializations before bean is ready to be used. Like opening database connections etc.

Performs processes that needs to be performed after been is initialized. Method used postProcessAfterInitialization() from BeanPostProcessor interface.

Bean Ready to Use:
Bean is ready to use by the application.

Destroy the Bean:
Bean is destroyed. Method used is destroy() from DisposableBean interface.

Life Cycle of Application Context:
Application context is built over Bean Factory, so application context will inherit all the capabilities of BeanFactory. In addition to that, Application context provides,

  • Support Internalization
  • Events to beans, which are registered Listeners


Steps are same as Bean Factory Life Cycle, But it has one Step between SetbeanFactory and Pre-Initialization – SetApplicationContext

This step is only valid if application Context is used. Method used is setApplicationContext() from ApplicationContextAware Interface.

Read More

Spring Architecture – An Introduction

Spring Architecture – An Introduction:
Spring is a lightweight framework used to build java applications. In this article let us see the Spring Architecture in detail.


There are seven modules in Spring,

  • Core
  • ORM
  • DAO
  • AOP
  • MVC
  • Application Context
  • Web Context

This forms the base of Spring framework. All the other modules are built over Core module. This module contains Bean factory container which forms the basis to implement Dependency Injection.

Data Access Object – This module provides a standard way for data access. (Hibernate, JDBC etc..).  Normally in an application to access the database we need to opening connection, perform the operation and close the connection. But in Spring DAO, We can overcome this issue’s because Spring takes care of opening and closing of database connections and all we have to do write code to perform the operations.

Object Relational Mapping – This helps us to integrate Spring framework with other Frameworks such as Hibernate, JDO etc.

Aspect Oriented Programming – Helps in implementing aspects within our application. Aspect Oriented Programming helps us to separate application logic from system level services.

Model View Controller – This module helps us to separate application logic from the view.

Application Context:
This module is built over Core Module. Application Context has all the concepts of Bean factory and in addition to those concepts application context has internalization of Messages, life cycle events and validation

Web Context:
Web Context is a part of web application which includes MVC module.

Read More

Spring Hibernate Integration Example – CRUD Operations

Spring Hibernate Integration Example – CRUD Operations
In this article, We are going to see how to perform CRUD Operations with the help of Spring and Hibernate. We are going to use Spring Dependency Injection concept and we are also going to declare Hibernate Properties in Spring Application Context XML file.

I have downloaded and used some jar files in this application,
Jar files Needed:
Other than this, Spring Framework, Hibernate and MySQL JDBC Driver are required

Here is the application Structure,







op1 op2

 Download Source code (Src Files – applicationContext.xml, pojo, DAO, DAOImpl and execution)




Read More

session.getAttribute and session.setAttribute in Spring MVC

session.getAttribute and session.setAttribute in Spring MVC
Here is an example of how to use session.setAttribute and session.getAttribute, the same way we use in Servlets.  (for Spring mvc simple form handling example-click here and for Multiple submit buttons in spring mvc form – click here).

In this example we are going to see how to use “session.setAttribute” and “session.getAttribute”. We are going to get username in page1.jsp and then control passes to page2.jsp and we are going to display the username in page3.jsp using session attributes.


















Read More