Java & Spring Framework
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
- 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.
Spring
- Spring is a framework that simplifies Java application development.
- 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
@SpringBootApplicationannotation in theCalculatorApplicationclass is a convenience annotation that provides auto-configuration, component scanning, and other Spring Boot features. - When the
CalculatorApplicationclass is executed, Spring Boot initializes the spring container, scans for components (like theCalculatorController), 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.
- 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.
- Bean Instantiation: When the spring container starts up, it reads the bean definition and creates instances of the beans.
- Dependency Injection: Often, beans depend on other beans to function properly. For example, we could have created
CalculatorServiceand its implementations would have been injected intoCalculatorController. - Bean Lifecycle: Spring manages the lifecycle of beans, including initialization and destruction.
- 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.
Spring MVC
Controller Layer:
- The
CalculatorControllerclass is a Spring MVC controller that handles HTTP requests. - Each method in the controller is mapped to a specific URL path using the
@GetMappingannotation. - 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
@RequestParamannotation is used to bind the request parameters (aandb) 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.
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.










