The interceptor model of Java implementation

Source: Internet
Author: User

Interceptors are common in popular open source frameworks, and rely on technologies that are dynamic proxies for Java.
Understanding the core principles of interceptors is critical to understanding the architecture of these open source frameworks.
The following is a simple model to illustrate the general approach to the implementation of the Interceptor.

The model is divided into the following modules:
Business component: Is an object that is represented and blocked.
Agent Processor: An object that implements the Invocationhandler interface
Proxy object: Proxy object.
Interceptor: A normal javabean that automatically intercepts and executes some of its own methods before or after calling a business method.
Client: The portal for performing business processing.


The following is the implementation of the model

One, business components: divided into business interface and business class

/**
* Created by IntelliJ idea.
* User:leizhimin
* date:2008-3-20 23:32:06
* Company:lavasoft (http://lavasoft.blog.51cto.com)
* Business Component Interface
*/
Public InterfaceBusinessinterface {
Public voidDoSomething ();
}



/**
* Created by IntelliJ idea.
* User:leizhimin
* date:2008-3-20 23:31:12
* Company:lavasoft (http://lavasoft.blog.51cto.com)
* Business Components
*/
Public classBusinessclass Implementsbusinessinterface{

Public voidDoSomething () {
System.out.println ( "Business Component Businessclass method call: DoSomething ()");
}
}


Second, the agent processor: contains the business object binding dynamic proxy class processing, and implementation of the Invocationhandler interface invoke method.


ImportJava.lang.reflect.InvocationHandler;
ImportJava.lang.reflect.Method;
ImportJava.lang.reflect.Proxy;

/**
* Created by IntelliJ idea.
* User:leizhimin
* date:2008-3-20 23:24:10
* Company:lavasoft (http://lavasoft.blog.51cto.com)
* Dynamic Agent Processor Tool
*/
Public classDynamicproxyhandler im

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.