Java Spring Development Journey
Spring Framework
Having worked in my current role for about 8 months, I have realized that to be a lynchpin, I need to understand Java programming, especially using Spring Framework and Angular front-end. Towards this end, I've spent a lot of my time and energy learning and honing this skill.
Spring Framework |
Spring MVC - Model-View-Controller:
Spring MVC |
- Model: Represents the data and the business logic of the application. It is responsible for retrieving data from databases, encapsulating it, and defining the business operations.
- View: This is the user interface of the application. It displays the data from the Model to the user and also sends user commands to the Controller.
- Controller: Acts as an intermediary between View and Model. It receives user input from the View, processes it (often with the help of Model objects), and returns the output display data to the View.
In Spring, the above 3 items are implemented as follows:
- Model: Typically implemented using plain old Java objects (POJOs) and Spring services.
- View: Often realized through JSPs, Thymeleaf templates, or other templating engines that render the UI. In very basic development, just HTML pages can also be utilized.
- Controller: Spring MVC controllers are annotated classes (with @Controller) that handle HTTP requests and responses. Can also be @RestController.
Model (user.java)
public class User {
private String name;
private int age;
// Constructors, getters, setters
}
Inversion of Control (IoC) and Dependency Injection:
Inversion of Control is a design principle where the control of objects or portions of a program is transferred to a container or framework. It's about inverting the flow of control compared to traditional programming methods, where the custom code makes the decisions and controls the flow of execution.
In the context of Spring, IoC means that the Spring Framework takes control of creating and managing the life cycle and configuration of application objects (beans). Instead of the application code directly instantiating or locating necessary services, Spring container does it.
Dependency Injection is a design pattern used to implement IoC. It allows a system to be more decoupled and modular by removing the dependency that a class might have on a specific implementation of an interface. In DI, dependencies (services, clients, etc.) are injected into objects (beans) by the container, rather than being hard-coded.
There are several methods of DI:
- Constructor Injection: Dependencies are provided through a class constructor.
- Setter Injection: The container injects dependencies through setter methods.
- Field Injection: The container injects dependencies directly into annotated fields.
public class MyApplication {
private MyService service = new MyServiceImpl(); // Directly creating the dependency
public void process() {
service.performAction(); // Using the dependency
}
}
In this example, MyApplication is directly creating and managing the instance of MyService. This is a tight coupling and makes testing and maintenance harder.
Comments
Post a Comment