to manage the trigger uniformly. This manager is invocation Handler.The dynamic proxy pattern has a slightly different structure than the static proxy pattern above, and introduces a invocationhandler role.First explain the role of Invocationhandler:
In a static proxy, the methods in proxy proxies specify the corresponding method in which a particular realsubject is called:
In the static proxy mode above, the thing that the proxy does is to invoke the method that trigger
On-line self-taught the next Java agent, put out the code to make a note and summaryFirst, the purpose of using the agent1. Route method calls to the remote server2. Associating user interface events and actions during program operation3, for debugging, tracking method calls and so onIi. the classes and interfaces required to implement the agent include:1. proxy
pieces of code.
*/
Xiangqininterface proxy = (xiangqininterface) proxy.newproxyinstance (
Zhangsan.getclass (). getClassLoader (),
Zhangsan.getclass (). Getinterfaces (),
New Readyinvocationhandler (Zhangsan));
Proxy.xiangqin ();
/**
* Here to explain the meaning of the long-length code in the middle, and what exactly did it work?
* First, according to Zhangsan.getclass (). getClassLoader () This class loader to proxy cla
"," EFG ");p Roxy2.killboss ();p roxy2.upgrade (); System.out.println ();/* Dynamic proxy: Using Invocationhandler provided by the JDK, reflection calls the method of the Proxied object * combined with Java.reflect.Proxy to generate the proxy object * Dynamically passing in the Proxied object constructs Invocationhandler, which can enhance the function of the method of the Proxied object in the invoke of the handler * or: (Face-tangent:) where (connection point), what behavior (notification) *
each httpurlconnection request, set the agent Serverusername and password. and Base64 encryption (see your proxy server requirements, may vary depending on the proxy server). Code such as the followingPrivate HttpURLConnection Getproxyhttpconnection () throws IOException {String authorization = Config.getproxyuser () + ": "+ Config.getproxypassword (); URL url = new URL (config.getrequesturl ()); HttpURLCo
When we learn spring, we know that spring has two main ideas, one is the IOC, the other is AOP, and for IOC, dependency injection doesn't have to be said, and for spring's core AOP, we not only need to know how to satisfy our function through AOP, What we need to learn is how the underlying is a principle, and the principle of AOP is the dynamic agent mechanism of Java, so this essay is a review of the dyna
First, the origin of dynamic agentsAgent refers to: to each specific class to write a proxy class, in the future to use a specific class, as long as the object to create its proxy class, and then invoke the proxy class method can be, but if there are many specific classes, it is necessary to create a lot of proxy class can, this is obviously not suitable, the dynamic agent was born.Second, there are 2 ways to implement dynamic
() {System. out.println("Byebye"); } });//Generate an implementation class object for the subject interfaceSubject Subject = (Subject) proxyfactory.createfactory();//through newinstance () to get a proxy object, this proxy object implements the subject interface, so it can be strongly converted to subject interface class. Bsubject Subject2 = (bsubject) proxyfactory.createfactory(); Object result = subject.OutPut( -);//corresponding to the Invocationhandler invoke method: Subject as proxy,
,filemanager,null,null,null,units);Task.call ();//CallFilemanager.close ();//************ The above process obtained the Java File source code, compiled Java file generated the corresponding class file ****************//*************** The following procedure to load a class file into memory, to generate a new object *****************************//class.load () i
class to execute the extension function and the original class method.Compare the classes that are generated after the original class and the dynamic proxy:1, $Proxy 0 accesses the real class object through the Invocationhandler implementation class call.2, dynamic Proxy extension is not included in $proxy0, but rather callback Invocationhandler interface, implement invoke method extension through subclass.Before and after using dynamic proxies from the invocation relationship:Left: is the orig
Before talking about the Java dynamic Agent, let's look at the proxy mode.The proxy class is enhanced by the combination, and the Hello class is delegated.Proxy Mode code: Public class Proxypattern {Interface ihello{void say ();}Static class Hello implements ihello{ Public void say () {System. out. println ("Hello World");}}Static class Proxy implements ihello{Ihello Ihello; Public Proxy (Ihello Hello) {Ihe
In the dynamic agent mechanism of Java, there are two important classes. One is Invocationhandler and the other is proxy.
invocationhandler: Each dynamic proxy class must implement the Invocationhandler interface, and each instance of the proxy class is associated with a handler, and when we call a method through the proxy, The invocation of this method is forwarded to the Invoke method of this interfa
for//Method : Refers to the methods object that we want to invoke the real object//args: Refers to parameters that are accepted when a method of a real object is called Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//enhanced before calling the method contentSYSTEM.OUT.PRINTLN ("Open Transaction"); Object Invoke=Method.invoke (us, args); //enhanced after calling this methodSystem.out.println ("Close Things"); returninvoke; } }4. Test classDemo.java Publ
time. The function that we call the proxy object is called timingInvocationHandler invoke (not a bit like a static proxy), whereas the function invoke implementation calls the corresponding method of the delegate class object new OperateImpl() (not a bit like a static proxy). public static Object newproxyinstance (ClassLoader loader, class loaderRepresents the class loaderinterfacesRepresents the interface of the delegate class, which is required to
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.