Understanding Entity Lifecycle

Entity Lifecycle In JPA (Java Persistence API), the entity lifecycle refers to the process from when an entity is created until it is destroyed. 4 States of Entity Lifecycle New/Transient: A new entity is created, but it is not yet managed by an EntityManager. It is not stored in the database and is not managed by the persistence context. Entities created with the new keyword are in the transient state. Managed: ...

June 8, 2024 · 2 min · 232 words · In-Jun Hwang

Exploring the Dispatcher Servlet

What is the Dispatcher Servlet? The Dispatcher Servlet is at the heart of Spring MVC. It is responsible for receiving client requests, delegating them to the appropriate controller, and then passing the result returned by the controller to the view. There is only one Dispatcher Servlet in a web application, and it handles all client requests. How the Dispatcher Servlet Works Receives a request from the client. Finds the controller that will handle the client’s request through Handler Mapping. Executes the controller through Handler Adapter. Transforms the result returned by the controller into a View through View Resolver. Forwards the view to the client. When using RestController, View Resolver is not used. Instead, the object is converted to JSON format and sent to the client. ...

June 5, 2024 · 3 min · 434 words · In-Jun Hwang

What is Spring Interceptor?

Spring Interceptor Interceptor performs similar role to Servlet Filter. Servlet filter provides functionality to intercept and process requests before and after they reach the servlet container. Spring Interceptor provides functionality to intercept and process requests before and after they reach the controller in Spring MVC. Interceptor is implemented using HandlerInterceptor interface. HandlerInterceptor interface provides three methods: preHandle: Method executed before the request reaches the controller postHandle: Method executed after the request has reached the controller, before the view is rendered afterCompletion: Method executed after the view has been rendered Interceptor Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CustomInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // Code to execute before the request reaches the controller // If false is returned, request is not forwarded to the controller return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // Code to execute after the controller is executed normally // Not executed if an exception is thrown } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // Code to execute after the view has been sent to the client // Exception object can be used to check exception information if an exception occurred // Exception information can be checked and logged } } Interceptor Registration To register the interceptor, implement the WebMvcConfigurer interface and override the addInterceptors method. ...

June 4, 2024 · 3 min · 567 words · In-Jun Hwang

What Is a Servlet Filter?

Servlets are Java classes which handle a client’s request. Servlets are used to process HTTP requests and to generate HTTP responses. What is a Servlet Filter? A Servlet filter provides a way to modify the request or response before it is handled by the servlet, or after the servlet has generated it. Why would you want to modify the request or response? There are many reasons to modify the request or response, such as: ...

June 4, 2024 · 5 min · 883 words · In-Jun Hwang

Spring Boot Development Guide: Component-based Development Order and Best Practices

Core Components of Spring Boot Applications Let’s explore the main components and their roles involved in developing Spring Boot applications: 1. Entity An object that maps to a database table. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String username; @Column(nullable = false) private String email; // getter, setter, constructor } 2. Repository An interface that handles database operations. ...

May 25, 2024 · 3 min · 488 words · In-Jun Hwang