Java Static agent and dynamic Agent summary

Source: Internet
Author: User
Tags throwable

Static proxy

First implementation (interface-based):

1 "Interface

Public interface Hello {
    void Say (String msg);
}

2 "target class, implement at least one interface

public class Helloimpl implements Hello {
public void Say (String msg) {
System.out.println ("Hi," +msg);
}
}

3 "proxy class (implements the same interface as the target class to ensure consistent functionality)

public class Helloproxy implements hello{

private Hello Hello;

Public helloproxy (Hello Hello) {
This.hello = Hello;
}

public void Say (String msg) {
Before ();
Hello.say (msg);
After ();
}
private void before () {
System.out.println ("before");
}
private void After () {
System.out.println ("after");
}
}

3 "Test

/**
* @Author LZHL
* @Create 2017-02-19 10:26
* @Description
*/
public class Main {
public static void Main (string[] args) throws Exception {
Helloimpl target = new Helloimpl ();
Helloproxy proxy = new Helloproxy (target);
Proxy.say ("Lzhl");
}
}

The second implementation (based on the target class):

1> Target Class

public class Hellotarget {
public void SayHello (String name) {
System.out.println ("Hi," +name);
}
}

2> proxy class (by inheriting the target class to ensure consistent functionality)

public class Helloproxy extends hellotarget{
Private Hellotarget target;
Public Helloproxy (Hellotarget target) {
This.target = target;

@Override
public void SayHello (String name) {
This.before ();
Target.sayhello (name);
This.after ();
}
private void before () {
System.out.println ("before");
}
private void After () {
System.out.println ("after");
}
}

3> Test

public class Main {
public static void Main (string[] args) throws Exception {

Helloproxy proxy= new Helloproxy (target);
Proxy.sayhello ("Lzhl");
}
}

Dynamic Agent

The proxy class of dynamic agent is generated dynamically during program running, and there are two implementations, one is JDK dynamic agent and one is Cglib dynamic agent

1 "JDK dynamic agent (based on interface implementation, with the target class implementation of the same interface, so as to ensure consistent functionality)

/**
* @Author LZHL
* @Create 2017-02-19 12:46
* @Description
*/
public class Main {
public static void Main (string[] args) {

Final Helloimpl target = new Helloimpl ();

Object proxyinstance = proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). GetInterfaces ( ), new Invocationhandler () {
/*
* Proxy: Agent Object
* Method: The target object's methods object
* Args: Parameters of the target object method
* Return: The returned value of the target object method
*/
public object invoke (object proxy, Method method, object[] args) throws Throwable {
System.out.println ("before");
Object RetValue = Method.invoke (target, args);
System.out.println ("after");
return retvalue;
}
});
Hello proxy = (hello) proxyinstance;
Proxy.say ("LYX");

Invocationhandler can be extracted, write a separate class, in order to facilitate everyone to see, here I use the form of internal classes
 class Jdkproxy implements Invocationhandler {
Private Object target;
Public Jdkproxy (Object target) {
This.target = target;

public Object Invoke (object proxy, Method method, object[] args) throws Throwable {
is Fore ();
Object result = Method.invoke (target, args);
after ();
return result;
}
private void before () {
System.out.println ("before");
}
private void After () {
System.out.println ("after");
}
}
Invocationhandler ih = new Jdkproxy (target);
Object ProxyInstance2 = proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). getinterfaces (), IH);
Hello Proxy2 = (hello) proxyInstance2;
Proxy2.say ("Lzhl");
}
}

2 "Cglib dynamic Proxy (based on the target class, by inheriting the target class to ensure that the function is consistent), need to import the Cglib-3.2.4.jar package

Pom.xml

<dependencies>
<!--Https://mvnrepository.com/artifact/cglib/cglib--
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>

</dependencies>

1) Target class

public class Hi {
public void Sayhi (String msg) {
System.out.println ("Hi," +msg);
}
}

2) Testing

/**
* @Author LZHL
* @Create 2017-02-19 13:19
* @Description
*/
public class Main {
public static void Main (string[] args) {
Enhancer enhancer = new enhancer ();
Set Parent class
Enhancer.setsuperclass (Hi.class);
Setting the callback function
Enhancer.setcallback (New Methodinterceptor () {
public object intercept (object target, Method method, object[] args, Methodproxy methodproxy) throws Throwable {
System.out.println ("before");
Object RetValue = Methodproxy.invokesuper (target, args);
System.out.println ("after");
return retvalue;
}
});
Object proxy = Enhancer.create ();
Hi hi = (hi) proxy;
Hi.sayhi ("Lxy");

Methodinterceptor can be extracted, write a separate class, in order to facilitate everyone to see, here I use the form of internal classes
Class Cglibproxy implements Methodinterceptor {

Public <T> T GetProxy (class<t> clazz) {
Return (T) enhancer.create (Clazz, this);
}
public object intercept (object target, Method method, object[] args, Methodproxy proxy) throws Throwable {
Before ();
Object result = Proxy.invokesuper (target, args);
After ();
return result;
}
private void before () {
System.out.println ("before");
}
private void After () {
System.out.println ("after");
}
}

Cglibproxy cglibproxy = new Cglibproxy ();
Hi Hi2 = Cglibproxy.getproxy (Hi.class);
Hi2.sayhi ("Lzhl");
}
}



Java Static agent and dynamic Agent summary

Related Article

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.