package Test2.service;
public interface HelloService {
int print(String name);
}
package Test2.service;
public class GongNeng implements HelloService {
@Override
public int print(String name) {
System.out.println("其他人写好的代码");
return 2;
}
}
package Test2.service.handler;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandlder implements InvocationHandler {
Object target = null;
public MyInvocationHandlder(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object res = method.invoke(target,args);
if(res !=null){
Integer i = (Integer) res;
res = i*2;
}
return res;
}
}
package Test2.service;
import Test2.service.handler.MyInvocationHandlder;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class MyAPP {
public static void main(String[] args) {
HelloService gn = new GongNeng();
InvocationHandler mi = new MyInvocationHandlder(gn);
HelloService proxy = (HelloService)Proxy.newProxyInstance(gn.getClass().getClassLoader(),
gn.getClass().getInterfaces(),mi);
Integer value = proxy.print("李四");
System.out.println(value);
}
}