package practice;
public class StaticProxyTest {
public static void main(String[] args) {
Proxy proxy = new Proxy(new HelloHanddler());
proxy.handdle();
}
static class MyLogger {
public static void before() {
System.out.println("logger before");
}
public static void after() {
System.out.println("logger after");
}
}
// interface
static interface Handdler {
void handdle();
}
// the business class implements the interface
static class HelloHanddler implements Handdler {
@Override
public void handdle() {
System.out.println("hello");
}
}
// the proxy class implements the interface
static class Proxy implements Handdler {
Handdler handdler;
public Proxy(Handdler handdler) {
this.handdler = handdler;
}
@Override
public void handdle() {
MyLogger.before();
handdler.handdle();
MyLogger.after();
}
}
}
package practice;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DynamicProxyTest {
public static void main(String[] args) {
Handdler handdler =(Handdler) new Proxy().bind(new HelloHanddler());
handdler.handdle();
}
static class MyLogger {
public static void before() {
System.out.println("logger before");
}
public static void after() {
System.out.println("logger after");
}
}
// interface
static interface Handdler {
void handdle();
}
// the business class implements the interface
static class HelloHanddler implements Handdler {
@Override
public void handdle() {
System.out.println("hello");
}
}
}
// the proxy class implements dynamic interface
class Proxy implements InvocationHandler {
Object target;
public Object bind(Object object) {
this.target = object;
//返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
return java.lang.reflect.Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
DynamicProxyTest.MyLogger.before();
Object result = method.invoke(target, args);
DynamicProxyTest.MyLogger.after();
return result;
}
}
package practice;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DynamicProxyTest {
public static void main(String[] args) {
Handdler handdler = (Handdler) new Proxy().bind(new HelloHanddler(), new MyLogger());
handdler.handdle();
}
// interface
static interface Handdler {
void handdle();
}
// the business class implements the interface
static class HelloHanddler implements Handdler {
@Override
public void handdle() {
System.out.println("hello");
}
}
}
class MyLogger {
public void before() {
System.out.println("logger before");
}
public static void after() {
System.out.println("logger after");
}
}
// the proxy class implements dynamic interface
class Proxy implements InvocationHandler {
Object target;
Object logger;
public Object bind(Object object, Object logger) {
this.target = object;
this.logger = logger;
// 返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
return java.lang.reflect.Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Class clz = logger.getClass();
Method before = clz.getMethod("before", null);
Method after = clz.getMethod("after", null);
before.invoke(logger, null);// invoke non static method
Object result = method.invoke(target, args);
after.invoke(null, null); // invoke static method
return result;
}
}
- 动态代理, 自定义注解(对注解的方法,使用动态代理添加切片逻辑)
package proxy;
public interface IPlan {
@Visit()
public void visit();
}
package proxy;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Visit {
}
package proxy;
public class PlanDalian implements IPlan{
@Override
public void visit() {
System.out.println("What a beautiful city.");
}
}
package proxy;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class VisitProxy implements InvocationHandler {
Object target;
public Object bind(Object obj) {
this.target = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getAnnotations() == null || method.getAnnotations().length == 0) {
return method.invoke(target, args);
}
Object result = null;
for (Annotation annotation : method.getAnnotations()) {
if (annotation instanceof Visit) {
System.out.println("Welcome");
result = method.invoke(target, args);
System.out.println("bye");
return result;
} else {
continue;
}
}
return method.invoke(target, args);
}
}
package proxy;
public class Main {
public static void main(String[] args) {
IPlan plan =(IPlan) new VisitProxy().bind(new PlanDalian());
plan.visit();
}
}
网友评论