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

How does Java implement the interception function of interceptor Interceptor?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how Java implements the interception function of interceptor Interceptor. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Interceptors in Java are objects that dynamically intercept action calls. It provides a mechanism that allows developers to define the code that is executed before and after the execution of an action, or to prevent the execution of an action before it is executed. It also provides a way to extract reusable parts of the action. In AOP (Aspect-Oriented Programming), interceptors are used to intercept a method or field before it is accessed, and then add certain actions before or after.

In addition, interceptors are common in popular open source frameworks, relying on dynamic proxies for Java. Understanding the core principles of interceptors is critical to understanding the architecture of these open source frameworks. Next, we will use a simple model to illustrate the general approach to interceptor implementation.

The model is mainly divided into five modules, respectively:

Business components, proxies and intercepted objects

The proxy processor, which implements an object of the InvocationHandler interface

Proxy object, Proxy object

Interceptors, ordinary Java Bean, automatically intercept and execute some of their own methods before or after calling business methods

The client, the entrance to perform business processing.

Next, we use the Java language to implement the interceptor Interceptor's interception function:

Step 1: create business component interface BusinessFacade/** * @ author VC fructose * @ create 2017-03-30 * * GitHub:github.com/guobinhit * * business component interface * / public interface BusinessFacade {public void doSomething () Step 2: create a business component implementation class BusinessClass/** * @ author VC fructose * @ create 2017-03-30 * * GitHub:github.com/guobinhit * * implementation class * / public class BusinessClass implements BusinessFacade {public void doSomething () {System.out.println ("call method in business component BusinessClass: doSomething ()") Step 3: create interceptor InterceptorClass/** * @ author VC fructose * @ create 2017-03-30 * * GitHub:github.com/guobinhit * * interceptor * / public class InterceptorClass {/ / call public void before () {System.out.println before action ("call method in interceptor InterceptorClass: before ()") } / / call public void after () {System.out.println ("call method in interceptor InterceptorClass: after ()" after action) Step 4: create the dynamic proxy processor tool DynamicProxyHandler/** * @ author VC fructose * @ create 2017-03-30 * * GitHub:github.com/guobinhit * * dynamic proxy processor tool * / public class DynamicProxyHandler implements InvocationHandler {/ / declare the proxied object private Object business; / / create interceptor private InterceptorClass interceptor = new InterceptorClass () / * dynamically generate a proxy class object and bind the proxied class and the proxy processor. * * @ param business * @ return proxy class object * / public Object bind (Object business) {this.business = business / * Proxy.newProxyInstance (parameter 1, parameter 2, parameter 3) * * parameter 1, which represents the ClassLoader * parameter 2 of the proxied class, the interface * parameter 3 of the proxy, and the proxy processor object * * this method Return proxy instance * / return Proxy.newProxyInstance (business.getClass () .getClassLoader (), business.getClass () .getInterfaces (), this) The method that the proxy needs to call, and calls the method of the connector before and after the method is called. * * @ param proxy proxy class object * @ param method proxied interface method * @ param args the result returned by the proxy interface method parameter * @ return method call * @ throws Throwable * / public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Object result = null; interceptor.before (); result = method.invoke (business, args) Interceptor.after (); return null;}} step 5: create client ClientDemo/** * @ author VC fructose * @ create 2017-03-30 * * GitHub:github.com/guobinhit * * client * / public class ClientDemo {public static void main (String args []) {/ / create dynamic proxy processing tool DynamicProxyHandler handler = new DynamicProxyHandler () / / create a business component object BusinessFacade business = new BusinessClass (); / / create a business component object and bind the proxy class BusinessFacade businessProxy = (BusinessFacade) handler.bind (business) with a dynamic proxy; / / call the methods in the business component to demonstrate the interceptor effect businessProxy.doSomething ();}}

Run the project code above, and the result is as follows:

As shown in the figure above, it is obvious that the interception function of our interceptor has been realized!

Through this article, we may have a more thorough understanding of how interceptors are implemented.

But, in real project practice, in order to implement the function of interceptor, we usually use inheritance class HandlerInterceptorAdapter or abstract class AbstractInterceptor, or implement HandleInterceptor interface.

In other words, we only need to care about how to rewrite the method, not its internal implementation principle.

The above is how Java implements the interception function of interceptor Interceptor. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report