Summarize the difference between filter and interceptor, the difference between static proxy and dynamic proxy

The difference between Filter and Intercepter:

  • The interceptor is based on Java's reflection mechanism, and the filter is based on function callbacks.
  • The interceptor does not depend on the servlet container, and the filter depends on the servlet container.
  • Interceptors can only work on action requests, while filters can work on almost all requests.
  • The interceptor can access the objects in the action context and value stack, but the filter cannot.
  • In the life cycle of the action, the interceptor can be called multiple times, while the filter can only be called once when the container is initialized
  • The interceptor can obtain each bean in the IOC container, but the filter cannot. This is very important. Injecting a service into the interceptor can call business logic.

In terms of flexibility, the interceptor function is more powerful, it can do everything Filter can do, and it can be executed before or after the request, which is more flexible. Filter is mainly for encoding URL addresses, filtering out useless parameters, and security verification (more general, such as logging in but not logging in). It is recommended to use interceptor for tasks that are too detailed. The specific one has to be selected according to different situations.
image description
Execution order:
before filtering-----before interception-----action processing-----after interception-----after filtering.
I personally think that filtering is a horizontal process. First, filter the content submitted by the client (for example, the processing that users who are not logged in cannot access internal pages); after the filtering is passed, the interceptor will verify the data submitted by the user and do some preliminary data Processing; Then send the processed data to the corresponding Action. After the Action completes and returns, the interceptor can do other things, and then return to the subsequent operation of the filter.

image description


The difference between dynamic proxy and static proxy:

According to the creation period of the agent, the agent class can be divided into two types: 

Static: The programmer creates a proxy class or a specific tool to automatically generate the source code and then compile it. The .class file of the proxy class already exists before the program runs.

Dynamic: It is created dynamically using reflection mechanism while the program is running.

Advantages and disadvantages of static proxy

advantage:

The proxy makes the client do not need to know what the implementation class is and how to do it, while the client only needs to know the proxy (decoupling). For the client code above, newUserManagerImpl() can be hidden by the factory, as above is just an example Just an example.

Disadvantages:

1) The proxy class and the delegate class implement the same interface, and the proxy class implements the same method through the delegate class. This resulted in a lot of code duplication. If an interface adds a method, in addition to all implementation classes that need to implement this method, all proxy classes also need to implement this method. Increased the complexity of code maintenance.

2) The proxy object only serves one type of object, if you want to serve multiple types of objects. It is bound to be a proxy for every kind of object, and a static proxy will not be competent when the program scale is a little larger. The above code only provides a proxy for the access of the UserManager class, but if we need to provide a proxy for other classes such as the Department class, we need to add the proxy class of the proxy Department again.

Advantages of dynamic agent:

Compared with static proxy, the biggest advantage of dynamic proxy is that all methods declared in the interface are transferred to a centralized method of invocation handler (InvocationHandler.invoke). In this way, when the number of interface methods is relatively large, we can deal with it flexibly, instead of needing to transfer each method like a static proxy. Moreover, the application of dynamic agents makes our class responsibilities more single and reusable.


  •  

Guess you like

Origin blog.csdn.net/tonglei111/article/details/115295935