8. The difference between Spring Aop, interceptor, and filter

The difference between Spring Aop , interceptor, and filter

Filter filter: Intercept web access url addresses.

Interceptor interceptor: intercepts urls ending with .action and intercepts Action access.

Spring AOP interceptor: Only access to Spring managed beans (business layer Service) can be intercepted

 

----------------------------------------------------------------------------

 

Spring AOP

 

Spring AOP , an implementation of AOP, uses the proxy mode.

 

Filter

 

Filter ( filter) is the specification of J2EE, Servlet2.3 began to introduce/implement the responsibility chain mode . Filter can be used to set the character set, control permissions, control steering and so on. Filter is also an implementation of AOP .

 

Interceptor

 

Interceptor ( interceptor) is a concept in Struct2 . It is also an implementation of AOP .

 

The difference between interceptors and filters:

 

  1. The interceptor is a Java -based reflection mechanism, and the filter is a Java-based function callback

 

  2. The interceptor does not depend on the servlet container, while the filter depends on the servlet container

 

  3. Interceptors can only work on action requests , and filters work on almost all requests

 

  4. The interceptor can access the action context and the objects in the value stack, while the filter cannot access it

 

  5. In the action life cycle, the interceptor can be called multiple times, and the filter can only be called once when the servlet is initialized

 

  6. The interceptor can obtain each bean in the IOC container, but the filter does not work. Injecting a service into the interceptor can call the logical business

Interceptor:

 

The filter in the servlet realizes the unified setting code and simplifies the operation; at the same time, it can also make logical judgments, such as whether the user has logged in, whether he has permission to access the page, and so on. It is started with the startup of your web application, it is only initialized once, and related requests can be intercepted later, and it is only destroyed when your web application is stopped or redeployed.

 

filter:

 

Interceptors are applied in aspect-oriented programming, which is to call a method before your service or a method, or call a method after the method. It is a reflection mechanism based on JAVA. The interceptor is not in web.xml, such as struts is configured in struts.xml,

 Summarize:

 

1. Filter: The so-called filter is used to filter as the name implies. In Java web , the request and response you pass in filter out some information in advance, or set some parameters in advance, and then pass in the action of servlet or struts for business Logic, such as filtering out illegal urls (not the address request of login.do, if the user is not logged in), or uniformly setting the character set before the action of the incoming servlet or struts, or removing some illegal characters (chat rooms often used, some swear words). The filter process is linear. After the url is sent and checked, the original process can be kept and executed downwards, received by the next filter, servlet, etc.

 

2. Java interceptors are mainly used in plug-ins, extensions such as hivernate springstruts2 , etc. are somewhat similar to slice-oriented technologies. Before using them, you must declare a paragraph in the configuration file, that is, the xml file.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325540920&siteId=291194637