Struts 2 Interceptor Bottom implementation Principle learning notes

Source: Internet
Author: User

Intercepting device:
1, first of all to have a target object, to intercept who
2, interception object, interceptor itself is just a common class
3, the target object generated by a proxy object, really to execute the proxy object, proxy object is also a number of methods
Generated by the system dynamics, not that we write out, the proxy object method is similar to the target object's method, but
The method of proxy object is to synthesize the method of interceptor and the method of target object of your system, combine it to form
Method of the object, which means that the proxy method is a combination of the Interceptor method and the target object method.

Example:
Add a class Target.java is our target object, before using the target object to have an interface, asking us to the interface
Programming, that is, Mr. Targetinterface into an interface, and generates a method that this interface is an ordinary interface; Target class implementation
Targetinterface interface, where target is the object, we have to intercept target.
The target has, now is to write a Interceptor object: Interceptor.java inside the Interceptor. We define two methods: before (),
After (), its interceptor and we usually see the class is not much different, the reason is called Interceptor is its behavior;
The dynamic proxy for Java objects is to use reflection; Generate a processor class MyHandler implement the Reflection interface Invocationhandler
The function is to inject the object to be processed into the processor class and invoke it dynamically.
The method Invoke () method in MyHandler is the method that is actually invoked.

Regenerate into a proxy class myproxy function is to generate an agent, method to GetProxy () to get an agent, where you want to use
proxy class, through newproxyinstance,new an agent to come out;

Clarify the relationship:
Target is the object, the target, the agent who
Interceptor is the interceptor that inserts the method dynamically before or after the target object method.
In some target and interceptor don't know each other, target doesn't know who is plugged in, and interceptor doesn't know where to insert it.
So in this declaration of a MyHandler processor, a real call to the target object method, before the target object method is invoked, executes the Interceptor's before

() After the target object method is invoked, the After () of the interceptor is executed;
There is also one of the most important proxy object myproxy, which has a getproxy () method that passes through the target object, where the target object is set into the processor

To Myhandler.setobject (object), and then returns a proxy instance that invokes the target object's method through the proxy instance, where the target object

The execution of the method calls the interceptor, which is the equivalent of intercepting the interceptor.


Summary Interceptor:
Role:
1, intercepted object or target, the target object's method is intercepted
2, the interceptor itself, the ordinary class classes, behavior up to say to intercept the role of others
3, agent, through the agent to generate a proxy for the target object, the execution is not the target object, but the agent, in the agent has been inserted in the interceptor

, such a function is to reduce the coupling
The target object is not associated with the interceptor itself, and the low coupling, coupled by Myhandler/myporxy, is through the XML configuration file in Struts2

To be coupled. The code path times are not coupled.

The bottom implementation of interceptor is dynamic proxy implementation.

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.