Sitemap

Everything about Spring Framework

12 min readJul 29, 2023

--

Java is distributed in three different editions:

1. Java Standard Edition ( Java SE ) :

Use to develop stand alone applications. A standalone application, also known as a desktop application is a software program designed in such a way that to run this software program, users don’t need an internet connection or any server access.

  • Greater Control : Standalone applications do not depend on outside servers to store their data, so users have more control over their data when using the standalone applications.
  • Cost-effectiveness : Standalone applications run on the local machines of users and do not require any server to store the data. So these types of applications are less expensive and easy to design and operate.

This is the minimum requirement to run a Java application. This edition provides a solid basis to the Java Enterprise Edition, and as such I will start by defining some of it’s components:

Java Virtual Machine (JVM) :

It is called a virtual machine because it doesn’t physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode. Java is a platform independent because of it’s JVM.

The JVM performs the following main tasks :

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment
Press enter or click to view image in full size

Java Class Library (JCL) :

The Java class library is a set of standard libraries that is available to any application running on JVM. This set of libraries is composed of classes that allow programs to handle common tasks like : network communication, collection manipulation, file operations, user interface creation, etc.

This standard library is also known as the Java standard Edition API.

Press enter or click to view image in full size

Java Runtime Environment ( JRE ) :

This Java Runtime Environment is a set of tools that provide an environment where java applications can run effectively. Whenever a user wants to run a java program, they must choose a vendor and install one of the versions available for their specific environment architecture.

There are two files that are worth noting on a typical JRE installation. The first one is the java executable file. This file is responsible for bootstrapping the JVM that will run the application. The second one is the rt.jar file. This file contains all the runtime classes that comprises the JCL.

Java Development Kit ( JDK ) :

The Java Development Kit is an extension of the JRE. Alongside with the files and tools provided by the JRE, the JDK includes compilers and tools to create java programs. For this reason, whenever one wants to develop a Java application, they need to install a JDK.

JDK

2. Java Micro Edition (J2ME) :

Used to build applications that run across all basic & smartphones.

Java ME (Micro Edition) is a platform designed to enable developers to build applications for mobile and embedded devices with limited resources. It provides a range of APIs that make it easier for developers to create applications for these devices.

3. Java Enterprise Edition ( J2EE ) :

Use to develop web application that run on servers example fb, twitter.

The Java Enterprise Edition was created to extend the Java SE by adding set of specifications that define capabilities commonly used by enterprise applications. The latest version of this edition contains over 40 specifications that help developers to create applications that communicate through web services, convert object-oriented data to entity relationship model, handle transactional conversations and so on.

One great advantage of having an enterprise edition defined as specifications, is that different vendors can develop their own application servers to support it. This leads to a richer environment where companies can choose the best vendor to support their operations.

One may wonder why companies like Oracle and Red Hat make available two versions of their applications servers: one open-source and free and the other paid and proprietary. The biggest differences between these versions are that the paid ones usually have more performance and better support. Vendors invest a lot to make these versions run smoothly and to solve any issues that might occur as fast as possible.

Java Enterprise Edition Features :

As already stated, Java EE comes with a lot (more than 40) features based on JSRs. These features help companies to handle common needs like persistence, security, web interfaces, state validation and so on. The following list enumerates some of the most important and used features of Java EE:

  • Java persistence API ( JPA ) : a specification for accessing, persisting and managing data between Java objects and a relational database.
  • Java Server Faces ( JSF ) : a specification for building component based user interfaces for web applications
  • Java Server Pages ( JSP ) : a technology that helps software developers to create dynamically generated web pages based on HTML.
  • Enterprise Java Beans ( EJB ) : a specification for developing components that encapsulates business logic of an application.

Pro’s :

  • It’s a core java with powerful set of libraries and API’s
  • Using those set of libraries and API’s, developers can build applications for business computing.

Con’s :

  • Implementation and usage of EJB (Enterprise Java Bean), is highly complex at initial version of JEE.
  • Poor performance of entity beans.

Why Spring ?

So keeping these con’s in the mind sir Rod Johnson introduced J2EE development without EJB. That means without using EJB we can create beans for J2EE development

Press enter or click to view image in full size

That’s where the concept of Spring came into picture

Press enter or click to view image in full size

Without EJB how can we develop Java Beans ?

Java Beans can be implemented using POJOS ( Plain Old Java ObjectS)

What is difference between Spring framework and Spring Boot ?

As a developer we will only develop a Spring framework application. We need to focus only on coding , all the configurations stuff will be taken care by spring boot.

Spring boot is something which will be providing production ready application. That means we can move the application to production just after initializing it. All configurations will be handled by the Springboot.

Spring Boot Features :

  • It will provide all configuration by default.
  • Also dependency jar file and plugins will be taken care.
  • By default embedded tomcat server is added in your application.
Press enter or click to view image in full size

Spring Framework Features :

  1. Inversion of Control ( IoC )
  2. Dependency Injection ( DI )
  3. Spring MVC (Model view control )
  4. Spring Data JPA
  5. Hibernate
  6. RESTful services
  7. AOP
  8. Spring Security,..etc.

1. Inversion of Control ( IoC ):

Approach for management of objects in the entire application and maintain configurable application.

Press enter or click to view image in full size

Go to https://start.spring.io website and there you can set up initialization for your projects. You can download the default application here on top of it you can develop your applications.

Steps to import your project into eclipse

Step 1 : Click on the downloaded jar file and click on the extract files here and select eclipse work place as path

Step 2 : After extracting in eclipse workplace, open eclipse and click on the file icon and select import, there select maven folder and select this as a maven project.

Step 3 : Browse your project and check whether the checkbox for pom.xml is checked or not

Step 4 : Click on finish

Get Avinash Dogiparthi’s stories in your inbox

Join Medium for free to get updates from this writer.

I’ll walk you through the files that are present in this sping boot project

  1. src/main/java → This is the place where you will create classes, packages and you will do actual implementation of this project here.
  2. src/main/resources → Here we will have application.properties files, our application related configurations are added in this file.
  3. src/test/java → This the place where you will test your classes that are implemented in first folder.
  4. JRE System Library → All java related packages will be in this folder.
  5. Maven Dependencies → All maven related libraries will be here like junit, springboot etc.
  6. src → We will be maintaing all the output files here.
  7. target → Once after developing our project we will build it, after building entire jar files of our application are generated here.
  8. HELP.md → You will be provided all the documentation related stuff in this file
  9. mvnw & mvnw.cmd → maven commands and maven command line
  10. pom.xml → It is the main file, in this file we will include all the dependencies, plugins that are useful for our project.

Before going further , i want all you to know what kind of project we are doing in this blog.

We need to create an interface first and then from that interface we are going to implement two different classes on top of that interface

Press enter or click to view image in full size

Creating an Interface

Step 1 : Right click on com.example.inversion_control package and select new and then select interface, a dialog box will appear there you need to name you interface.

Step 2 : Create a method called getModelAndColor() inside the interface, remember you don’t need to include implementation of that method since it is an interface.

Creating a class on top of Interface

Step 1 : Right click on the same package where you created interface and select new and then select class so while naming the class just below you need to add interface.

Press enter or click to view image in full size

So by doing this by default it will ovverride the methods that are present in the interface.

Step 2 : Please create another class by following the same, i have named by classes as Samsung and other one as Iphone.

Step 3 : System.out.println(“Model : iphone14”); implement this inside the method of iphone class and implement System.out.println(“Model : Samsungs22”); implement this inside the method of samsung class.

Step 4 : Now we are going to implement the methods of a particular class in the main java application i.e, InversionControlApplication.

Step 5 : So how to do that , we need to create objects of particular class and then we are going to access the methods that are present inside the objects of a particular class .. let’s see how to do that

Press enter or click to view image in full size

Here we are creating objects manually, so what’s our spring boot concept it is EJB. So we need to create all these objects as beans and we can use those beans whenever and wherever we want using spring annotations.

Lets say we did some implementation for one class object and we need to do the same thing for another class, then we need to create another object and need to do same implementation again… let’s solve this thing using spring

Bean is nothing but a simple java object there are no on top of definition for this.

Inversion of Control

How to Create a Bean?

Step 1 : To create a java bean we need to have a configuration class file

Step 2 : You need to follow same procedure you followed while creating new class but don’t need to add you interface now.

Step 3 : Let’s name this configuration class file as MobileConfig.

Step 4 : Add @Configuration annotation on top of your class, if you hover it then it will suggest you to import respective spring boot library, click on import.

Press enter or click to view image in full size

Step 5 : Now create a method in this class and return the Samsung object, So how to define it as bean, you just need to add @Bean on top of this method.

Press enter or click to view image in full size

Step 6 : To made these beans accessible inside your java application you need to add a bean container here

To create a bean container you need to add AnnotationConfigApplicationContext, to do this you need to import it

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

after importing it, now you need to create a object of this class and inside it you need to add where these beans should lie , in our case it is MobileConfig.class

Press enter or click to view image in full size

Let’s call the method of Iphone without creating it’s object again

Press enter or click to view image in full size

Is it configurable ? → definitely yes

Let’s see by creating some default constructor for both Iphone and Samsung

package com.example.inversion_control;

public class Iphone implements Mobiles {
public Iphone() {
System.out.println("Iphone constructor got triggered");
}

@Override
public void getModelAndColor() {
System.out.println("Model : Iphone14");

}

}
package com.example.inversion_control;

public class Samsung implements Mobiles {

public Samsung() {
System.out.println("Samsung constructor got triggered");
}
@Override
public void getModelAndColor() {
System.out.println("Model : Samsung S22");

}

}

Lets call method inside the object again

Press enter or click to view image in full size

So here you can see that both default constructors of Samsung class and Iphone got triggered, this happened because of Bean class. When ever we are using a bean class, it will create all the objects that are lying in the bean class and it will store them in cache memory, it will make accessible of all objects in future.

What is Dependency Injection ?

Now let’s say we have a scenario where we need to get color of both iphone and samsung after calling the getModelAndColor() method, and all the color properties are defined in another class called Color.class

Press enter or click to view image in full size

Now how do you use this getSamsungColor() method in respective classes like samsung or iphone , by creating an object of Color class inside the Samsung class right? ((since you cannot access that method without having a reference object of that particular class), that means your Samsung class is dependent on Color class.

To resolve this thing we have two different ways

  1. Constructor dependency injection : while creating object itself we will pass some values.
  2. Setter dependency injection : If you want to send data manually we can use setter dependency.

Constructor dependency injection :

Always remember ,

Color color; → this means here we have only initiated reference variable

Color color = new Color(); → this means here we created an object of color class.

Step 1 : Initialize the reference variable of Color class inside Samsung class

Step 2 : Inject the dependency in the bean class that means while creating the Samsung object

Press enter or click to view image in full size

Here it is raising an error since we don’t have a parameterized constructor inside the Samsung class of that particular type.

Step 3 : Create a parameterized constructor with parameter of Color Class object inside the Samsung class.

Press enter or click to view image in full size

Step 4 : Use the Color class getSamsungColor() inside the getModelAndColor() method implementation in Samsung Class

Press enter or click to view image in full size

Step 5 : Now run our java console application

Press enter or click to view image in full size

Like this you need to follow same steps , for Iphone Class also.

Spring MVC

Open Spring Initializer and Select dependencies that are useful for this section

Press enter or click to view image in full size

By Selecting Spring Web, it uses Apache Tomcat as the default embedded container.

Press enter or click to view image in full size

It will throw an error saying application failed to restart .. since in 8080 port some other application may be running .. Let’s reslove this thing by changing port number in application.properties file.

Now you will be able to see your server is running on 8081 port

Press enter or click to view image in full size

When ever if you want to develop a web application, you need to go to

src →main →New → Folder → webapp

This webapp will act as a default folder, by creating like this we don’t need to use additional configurations, while running the application directly they will read from this specific directory.

--

--

Written by Avinash Dogiparthi

Be productive to yourself everyday ✨️