The Java Foundation strengthens the agent

Source: Internet
Author: User

This article refers to self-http://www.cnblogs.com/xdp-gacl/p/3971367.html

1. What is an agent

Dynamic Agent technology is the most important technology in the whole Java technology, it is the basis of Learning Java framework, not dynamic proxy technology, then learning spring these frameworks are not understood.

   Dynamic Agent technology is the proxy object used to produce an object . Why do I need to generate proxy objects for an object in development?
Take a real-life example: singers or stars have their own brokers, the broker is their agent, when we need to find a star performance, we can not directly find the star, can only be a star agent. For example, Andy Lau in real life is very famous, will sing, will dance, will film, Andy Lau in no famous before, we can directly find him singing, dancing, filming, Andy Lau famous, he did the first thing is to find a broker, this agent is Andy Lau's agent (agent), When we need to find Andy Lau performance, can not directly find Andy Lau (Andy Lau said, you find my agent to discuss specific matters!), only to find Andy Lau's agent, so Andy Lau, the value of this agent is to intercept our direct access to Andy Lau!
This real-world example is the same as we do in development, where we create a proxy object for an object that is primarily used to intercept access to real business objects. So what should be the method of the proxy object? The proxy object should have the same method as the target object

So here are two concepts that explicitly represent the proxy object:
    1. The value of proxy object is mainly used to intercept access to real business objects .
2. the proxy object should have the same method as the target object (the real business object) . Andy Lau ( Real business object ) will sing, will dance, will be filming, we can not directly find him singing, dancing, filming, can only find his agent ( Agent object ) singing, dancing, filming, If a person wants to be Andy Lau's agent, then he must have the same behavior as Andy Lau (can sing, Can Dance, will film), Andy Lau has what method, he (agent) will have what method, we find Andy Lau's agent singing, dancing, filming, but agents do not really know how to sing, dance, filming, Really know how to sing, dance, filming is Andy Lau, in the real example is that we want to find Andy Lau singing, dancing, filming, then can only find his agent, pay his agent, and then let Andy Lau to sing, dance, filming.

2. Classification of agents

Static proxies: The relationship between the proxy class and the delegate class is determined before the code is run, meaning that the code for the proxy class already exists at the beginning.

Dynamic proxy: The byte code of the dynamic proxy class is generated when the program runs.

Agent in 3.Java " Java.lang.reflect.Proxy"Class Introduction

now to build a proxy object for an object, this proxy object is usually written with a class to generate , so first write the class used to generate the proxy object. In Java, how to use the program to generate a proxy object for an object, Java after JDK1.5 provides a "java.lang.reflect.Proxy" class, through the "Proxy" class provides a The Newproxyinstance method is used to create a proxy object for an object, as follows:

Static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h)

The newproxyinstance method is used to return a proxy object, which has a total of 3 parameters, and ClassLoader loader is used to indicate which class loader the build proxy object uses, class<?>[] The interfaces is used to indicate which object is generated by the proxy object, specified by the interface, Invocationhandler h is used to indicate what the resulting proxy object will do. So we just need to call the newproxyinstance method to get a proxy object for an object.

To write a class that generates a proxy object

in Java, if you want to produce an object's proxy object, then this object must have an interface , so our first step is to design the interface of this object, and define the behavior of the object in the interface (method)

stduentinterface.java-- defining the behavior interface of an object

 Public Interface Stduentinterface {    publicint  insertstudent (Student Student);}

studentdao.java-- Defining the Target business object class

 Public class Implements stduentinterface {    @Override    publicint  insertstudent (Student Student) {         // The following code is the business logic        return null = = student? 0:1;}    }

Creating a static Proxy

 Public classStudentstaticproxydao {PrivateStudentdao Studentdao;  PublicStudentstaticproxydao (Studentdao Studentdao) { This. Studentdao =Studentdao; }         Public intinsertstudent (Student Student) {logger.info ("Start inserting a student record:" +student.tostring ()); intNumber =studentdao.insertstudent (student); Logger.info ("Insert" + number + "bar student Record"); returnNumber ; }}

Dynamic Agent

 Public classLoggerdynamicproxyImplementsinvocationhandler{PrivateObject Target;  Publicloggerdynamicproxy (Object obj) { This. target =obj; }        /*** Invoke method in the dynamic proxy class * * Three parameters Meaning: * * Proxy: Proxy Object * Method: Delegate class to invoke methods * Args:meth Parameters of the Od method*/@Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {logger.info ("Before before business method call"); Object obj=Method.invoke (target, args); Logger.info ("Before before business method call"); returnobj; }}

Test class

 Public classProxytest { Public Static voidMain (string[] args) {/*** Basic Component Preparation*/Studentdao Studentdao=NewStudentdao (); Student Student=NewStudent (100000); Studentstaticproxydao Studentproxydao=NewStudentstaticproxydao (Studentdao); /*** Function of the test delegate class*/System.out.println (studentdao.insertstudent (student)); /*** Ability to test static proxy classes*/System.out.println (studentproxydao.insertstudent (student)); /*** Ability to test dynamic proxy classes*/stduentinterface newproxyinstance=(Stduentinterface) proxy.newproxyinstance (Studentdao.getclass (). getClassLoader (), STUDENTDAO.GETCLA SS (). Getinterfaces (),NewLoggerdynamicproxy (Studentdao)); intInsertstudent =newproxyinstance.insertstudent (student); System.out.println (insertstudent);}

Run results

1INFO: Start inserting a student record: Student [ID=100000, name=null1 student record 1info:before Business method calls before the Info:before business method call before the 1 Info:before business method call before theInfo:before business method call 1

The Java Foundation strengthens the agent

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.