package com.zhl.AopDemo_Maven;
public interface A {
public void add();
}
package com.zhl.AopDemo_Maven;
public class AImp implements A {
public void add() {
// TODO Auto-generated method stub
System.out.println("Execute()");
}
}
package com.zhl.AopDemo_Maven;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import sun.misc.ProxyGenerator;
/**
* Hello world!
*
*/
class Invok implements InvocationHandler
{
public Object obj;
public Invok(Object obj) {
// TODO Auto-generated constructor stub
this.obj = obj;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("before开始调用方法");
method.invoke(this.obj, args);
System.out.println("after结束调用方法");
return null;
}
}
public class App {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Object aImp =(Object)(Class.forName("com.zhl.AopDemo_Maven.AImp").newInstance());
Class<?>[] cls = aImp.getClass().getInterfaces();
for( Class<?>cl : cls)
{
String className = cl.getName();
System.out.println(className);
}
Invok ivk = new Invok(aImp);
A a = (A)(Proxy.newProxyInstance(aImp.getClass().getClassLoader(), aImp.getClass().getInterfaces(), ivk));
a.add();
}
}
输出结果:
com.zhl.AopDemo_Maven.A
before开始调用方法
Execute()
after结束调用方法
即在方法前和方法后调用输出信息,日志代理就是此模式。
加上System.getProperties().put(“sun.misc.ProxyGenerator.saveGeneratedFiles”, “true”);输出中间代理类字节码文件,经过反编译得到代理为:
package com.sun.proxy;
import com.zhl.AopDemo_Maven.*;
import java.lang.reflect.*;
public final class $Proxy0 extends Proxy implements A
{
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy0(final InvocationHandler invocationHandler) {
super(invocationHandler);
}
public final boolean equals(final Object o) {
try {
return (boolean)super.h.invoke(this, $Proxy0.m1, new Object[] { o });
}
catch (Error | RuntimeException error) {
throw;
}
catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
public final String toString() {
try {
return (String)super.h.invoke(this, $Proxy0.m2, null);
}
catch (Error | RuntimeException error) {
throw;
}
catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
public final void add() {
try {
super.h.invoke(this, $Proxy0.m3, null);
}
catch (Error | RuntimeException error) {
throw;
}
catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
public final int hashCode() {
try {
return (int)super.h.invoke(this, $Proxy0.m0, null);
}
catch (Error | RuntimeException error) {
throw;
}
catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
static {
try {
$Proxy0.m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
$Proxy0.m2 = Class.forName("java.lang.Object").getMethod("toString", (Class<?>[])new Class[0]);
$Proxy0.m3 = Class.forName("com.zhl.AopDemo_Maven.A").getMethod("add", (Class<?>[])new Class[0]);
$Proxy0.m0 = Class.forName("java.lang.Object").getMethod("hashCode", (Class<?>[])new Class[0]);
}
catch (NoSuchMethodException ex) {
throw new NoSuchMethodError(ex.getMessage());
}
catch (ClassNotFoundException ex2) {
throw new NoClassDefFoundError(ex2.getMessage());
}
}
}
在 代理类中, public final void add() {
try {
super.h.invoke(this, $Proxy0.m3, null);
}
catch (Error | RuntimeException error) {
throw;
}
catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
代理类调用的add方法,调用Invock中的add方法,输出提示信息,调用被代理类的add方法