Spring MVC Hibernate MySQL – Multiple Controllers

Spring MVC Hibernate MySQL – Multiple Controllers:
In our previous example we implemented Spring MVC Hibernate MySQL CRUD Operations (Here). In this article we are going to see how to use Multiple Controllers using Spring MVC.

In the below example, We are going to create 2 entities Employee and Manager and will have separate controllers for both these entities. Let us see how to add, view and delete. Edit operation is same as we discussed in previous example (Here), So i have not implemented Edit operation in this example.

Project Structure:

POM.xml

com.javainfinite.model:
Employee.java

Manager.java

com.javainfinite.dao:
EmployeeDAO.java

ManagerDAO.java

EmployeeDAOImpl.java

ManagerDAOImpl.java

com.javainfinite.service
EmployeeService.java

ManagerService.java

EmployeeServiceImpl.java

ManagerServiceImpl.java

spring-config.xml

web.xml

employee.jsp

manager.jsp

Output:

After Adding,

Deleting Employee,

On Clicking Manager Link,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Read More

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:

pom.xml

Employee.java

EmployeeDAO

EmployeeDAOImpl.java

EmployeeService

EmployeeServiceImpl

EmployeeController.java

 

spring-config.xml

web.xml

employee.jsp

edit.jsp

Output:

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.

beanfactory
Instantiate:
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.

Pre-Initialization:
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.

Post-Initialization:
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

applicationcontext

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.

architecture

There are seven modules in Spring,

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

CORE:
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.

DAO:
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.

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

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

MVC:
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:
org.apache.commons.dbcp.jar
commons-pool-1.6.jar
Other than this, Spring Framework, Hibernate and MySQL JDBC Driver are required

Here is the application Structure,
structure

javainfinite.pojo

javainfinite.DAO

javainfinite.DAOImpl

applicationContext.xml

javainfinite.execution

Output:

op1 op2

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

 

 

 

Read More