Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions of interceptors and filters of the Spring framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what are the functions of interceptors and filters in the Spring framework?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Filter (Filter): it depends on the servlet container. In implementation, based on the function callback, it can filter almost all requests, but the disadvantage is that a filter instance can only be called once when the container is initialized. The purpose of using the filter is to do some filtering operations to obtain the data we want, for example, in javaweb, filter out some information or set some parameters in advance for the incoming request and response, and then input servlet or controll for business logic operations. Commonly used scenarios: modify the character encoding in the filter, modify some parameters of HttpServletRequest in the filter (XSSfilter custom filter), such as: filtering vulgar text, dangerous characters, etc.

Interceptor (interceptors): it depends on the web framework, which in SpringMVC is dependent on the SpringMVC framework. In implementation, the reflection mechanism based on Java is an application of aspect-oriented programming (AOP), that is, before a service or a method, a method is called, or after a method, a method is called. For example, a dynamic proxy is a simple implementation of the interceptor, printing a string before calling the method (or doing other logical operations), or printing a string after calling the method. Even do business logic operations when an exception is thrown. Because the interceptor is based on the invocation of the web framework, some business operations can be done using Spring's dependency injection, and an interceptor instance can be called multiple times in a controller life cycle. However, the disadvantage is that it can only intercept controller requests, but there is no way to intercept other requests such as direct access to static resources.

Difference:

1. The scope of use is different: filter is stipulated by servlet specification and can only be used in web programs, while interceptors can be used in both web programs and application,swing programs.

2. Different specifications: filter is defined in the servlet specification and is supported by the servlet container. The interceptor is in the Spring container and is supported by the Spring framework.

3. Different resources are used: like other code blocks, the interceptor is a component of Spring, which is managed by Spring and configured in the Spring file, so you can use any resources and objects in Spring, such as service objects, data sources, transaction management, etc., which can be injected into the interceptor through Ioc, while Filter cannot

4. Different depth: Filter only works before and after Servlet. The interceptor can go deep into the front and back of the method, before and after the exception is thrown, etc., so the use of the interceptor is more flexible. So in Spring framework programs, interceptors should be given priority.

Implementation of the filter:

Define a class that implements the javax.servlet.Filter interface, which provides three methods

Init (FilterConfig config) method: this method is used for initialization and is automatically called when the container loads and instantiates the filter. The container passes a FilterConfig object for this method that contains configuration information.

DoFilter (servletRequest request,ServletResponse response,FilterChain chain) method: this method is the core method of the filter and is used to process requests and responses.

Destiry () method: this method is used to destroy the filter, but is called automatically before the container destroys the filter instance

There are two ways to configure filters:

1. Notes:

@ WebFilter defines a class that implements the javax.servlet.Filte interface as a filter

The name of the property filterName declaration filter, optional

The attribute urlPatterns specifies the URL mode to filter, or you can declare it using the attribute value

2. Configuration file

Configure in the web.xml file using tags

Implementation of the interceptor:

Define a class by implementing the HandlerInterceptor interface, or by inheriting the implementation class of the HandlerInterceptor interface, which implements three methods

PreHandle () method: this method is executed before the controller method, and its return value indicates whether to interrupt subsequent operations. When the return value is true, it means to continue to execute downward. When the return value is false, all subsequent operations will be interrupted.

PostHandle () method: this method is executed after the controller method call and before parsing the view, which allows you to make further changes to the model and view in the request domain

AfterHandle () method: this method is executed after the entire request is completed, that is, after the view rendering is completed, and you can use this method to clean up resources, log information, and so on.

There are two ways to configure interceptors:

1. In the SpringMVC configuration file, use tags to configure

2. Define a configuration class, inherit WebMvcConfigurerAdapter, override the addInterceptors method, and register the custom interceptor.

This is the end of the content of "what are the interceptors and filters of the Spring framework?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report