Coupling | Tight Coupling vs Loose Coupling | Examples

Coupling | Tight Coupling vs Loose Coupling | Examples

What is Coupling?
When one object is used by another object it is known as Coupling. In general, it is based on dependency. One object dependency over another object.

There are two types of Coupling,

  • Tight Coupling
  • Loose Coupling

First let us see about tight coupling,

Tight Coupling:

When one object is dependent very much over another object it is known as Tight Coupling. Let us understand with an Example.

For understanding this example, let us create an interface first,

We have a Job Interface with a method display in it.

Now to understand this example, let us create 2 classes. Doctor and Engineer which implements the Job Interface.

Doctor.java

Engineer.java

App.java

Now in main class,

This will give us the output,

So far so good, right?

Now let us consider the scenario, The requirement changes in future. We might have to add another professional for example Scientist. What we do now?

We need to create a class, implement job interface and override display method.

In App.java, we need to give reference to Scientist, add the Scientist variable to Constructor and create a new object and pass Scientist Object and then call display() method in App. java.

Too much of work right?

This is known as tight coupling. One object too much dependent and coupled with another object.

So how can we overcome this, here we are going to use concept of Loose coupling.

By this time, you might have guessed – Reducing the dependencies between object is known as Loose Coupling.

Now for the above same requirement with Doctor and Engineer, let us see how can we write the main class with Loosely Coupling method,

Loose Coupling:

Reducing the dependencies of one object with another object is known as Loose Coupling.

The above main method is re-written again with concept of Loose Coupling.

This code looks simpler when compared to Tight Coupled Code.

Instead of passing Doctor and Engineer objects directly, we have passed Job as reference.

During the run time we are passing the required object to perform the process. In this case, even if the requirement changes in future we do not have to make much changes for the above code.

Even when a new profession is introduced, example : Scientist. Now it is easy for us to make code changes without much change.

 

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,

 

 

 

 

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.

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.

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)