Sitemap
Ksheer Sagar Agrawal

Ksheer Sagar Agrawal

UCSD'27 (ML/DS) | SDE @ AWS DyanmoDB | IIT Gn '24 (EE+CS)

Java & Spring Framework

4 min readJun 4, 2024

--

Online Calculator

I wanted to build something on IntelliJ using spring framework. Here is my attempt to build an Online Calculator Project using Spring Boot and Gradle.

I’ve shared all the resources that I read to build this project and what did I learn.

JDK vs JRE vs JVM

  1. JDK contains a compiler that compiles your Java code (.java files) into byte code (.class files). JDK also contains JRE.

2. JRE contains libraries that provide pre-written code for common tasks like logging library.

Get Ksheer Sagar Agrawal’s stories in your inbox

Join Medium for free to get updates from this writer.

3. JVM takes the byte code and translates it into instructions that the underlying OS can understand. In short, it helps run the Java bytecode.

Press enter or click to view image in full size
Press enter or click to view image in full size
To learn more, James Hartman has written a wonderful article on this.

Spring

  1. Spring is a framework that simplifies Java application development.
  2. Spring Boot makes it even easier to set up and configure spring applications.

To get started with spring, I developed an online-calculator. It is based on MVC software architecture. I will explain MVC in a minute.

Spring Boot

  • Spring Boot is a framework that simplifies the setup and configuration of a Spring application.
  • The @SpringBootApplication annotation in the CalculatorApplication class is a convenience annotation that provides auto-configuration, component scanning, and other Spring Boot features.
  • When the CalculatorApplication class is executed, Spring Boot initializes the spring container, scans for components (like the CalculatorController), and sets up the necessary infrastructure (e.g., embedded Tomcat server).

Beans and Dependency Injection

In spring, a bean is simply an object that is managed by Spring Container. The spring container creates, initializes, and injects dependencies into beans. Beans are building blocks of applications and they can be anything from simple java objects to more complex components like controllers, services, repositories, etc.

  1. Bean definition: Before spring can create a bean, it must know how to create it. This information is provided in XML files, Java annotations, or Java code.
  2. Bean Instantiation: When the spring container starts up, it reads the bean definition and creates instances of the beans.
  3. Dependency Injection: Often, beans depend on other beans to function properly. For example, we could have created CalculatorServiceand its implementations would have been injected into CalculatorController.
  4. Bean Lifecycle: Spring manages the lifecycle of beans, including initialization and destruction.
  5. Bean Scopes: Beans can have different scopes that determine how many instances of the bean should be created.
  • singleton: Only one instance of the bean is created and shared across the entire application context.
  • prototype: A new instance of the bean is created every time it's requested.
  • request, session, and other web-specific scopes are also available for web applications.
Press enter or click to view image in full size
To learn more, TheTechDue has written Spring Bean Lifecycle Full Guide.

Spring MVC

Controller Layer:

  • The CalculatorController class is a Spring MVC controller that handles HTTP requests.
  • Each method in the controller is mapped to a specific URL path using the @GetMapping annotation.
  • When a client (e.g., a web browser or a REST client) sends a GET request to one of the mapped URLs (e.g., /add, /subtract, /multiply, or /divide), the corresponding method in the controller is called.
  • The @RequestParam annotation is used to bind the request parameters (a and b) to the method parameters.
  • The controller methods perform the requested arithmetic operation and return the result.

Model and View Layers:

  • In this project, there is no explicit Model or View layer because it’s a simple REST API.
  • The controller methods directly return the result of the arithmetic operation as a response.
  • In a more complex application, the Model layer would handle the data and business logic, while the View layer would be responsible for rendering the user interface.
Press enter or click to view image in full size
To learn more, TheTechDue has written Spring MVC Full Guide.

API Hit and Request Processing

When a client sends an HTTP GET request to one of the mapped URLs (e.g., http://localhost:8080/add?a=2&b=3), the request goes through the following steps:

a. The embedded Tomcat server receives the incoming request.

b. Spring's DispatcherServlet handles the request and routes it to the appropriate controller method based on the URL mapping.

c. The controller method is executed, and its return value is used as the response body.

d. The response is sent back to the client.

--

--

Ksheer Sagar Agrawal

Written by Ksheer Sagar Agrawal

UCSD'27 (ML/DS) | SDE @ AWS DyanmoDB | IIT Gn '24 (EE+CS)