Câu hỏi phỏng vấn Spring
Câu hỏi

What is Spring MVC Inter...

Câu trả lời

Spring MVC Interceptors are key components within the Spring Web MVC framework that allow developers to process HTTP requests both before they reach a controller and after the response has been constructed. They are used to implement cross-cutting concerns such as logging, authentication, and performance measurement.

Understanding Spring MVC Interceptor

Definition and Role

A Spring MVC Interceptor is used to intercept client requests and process them before they are handed over to the controller handler methods. This is particularly useful for tasks that need to be performed repeatedly across multiple requests, such as logging, security checks, data preprocessing, and post-processing of the response data[1][3].

How It Works

Interceptors work by defining specific methods that are executed at different stages of the request processing lifecycle:

  • preHandle(): This method is called before the actual handler (controller) is executed. It can be used to perform operations like authentication or logging. If this method returns true, the request continues to the next interceptor or the handler method. If it returns false, the request processing is aborted[1][3][6][7].
  • postHandle(): Executed after the handler method is invoked but before the view is rendered. This method can modify the ModelAndView object, allowing for additional data to be passed to the view or for the view to be changed altogether[1][3][7].
  • afterCompletion(): Called after the complete request has finished. It is used for cleanup activities, such as releasing resources, and is executed even if the request completes abruptly due to an exception[1][3][7].

Configuration and Usage

To use an interceptor in a Spring MVC application, you need to register it as part of the web application's configuration. This can be done either through XML configuration or Java configuration.

XML Configuration

In XML configuration, interceptors are declared within the <mvc:interceptors> tag. You can specify the interceptor to apply to all URIs or restrict it to specific URI patterns[1][9].

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/path/**"/>
        <b...
senior

senior

Gợi ý câu hỏi phỏng vấn

middle

What is Aspect?

middle

Describe some of the standard Spring events

junior

What in the world are Spring beans?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào