java动态代理(AOP)
2020-12-13 05:04
标签:ade 实现类 使用 class override 调用接口 package interface oca 一、使用jdk自带的动态代理机制: 执行结果为: before service java动态代理(AOP) 标签:ade 实现类 使用 class override 调用接口 package interface oca 原文地址:https://www.cnblogs.com/silenceshining/p/11129688.html
package aopdemo.jdk;
public interface INormalService {
public void service();
}
package aopdemo.jdk;
public class NormalServiceImpl implements INormalService {
@Override
public void service() {
System.out.println("invoke service");
}
}
package aopdemo.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class MyInvocationHandler implements InvocationHandler {
Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("before service");
Object object = method.invoke(target, args);
System.out.println("after service");
return object;
}
}
package aopdemo.jdk;
import java.lang.reflect.Proxy;
public class JdkProxyDemo {
public static void main(String[] args) {
INormalService normalService = new NormalServiceImpl();
MyInvocationHandler handler = new MyInvocationHandler(normalService);
INormalService iNormalService = (INormalService) Proxy
.newProxyInstance(normalService.getClass().getClassLoader(),
normalService.getClass().getInterfaces(), handler);
iNormalService.service();
}
}
invoke service
after service
上一篇:redis和memcached有什么区别?redis的线程模型是什么?为什么单线程的redis比多线程的memcached效率要高得多(为什么redis是单线程的但是还可以支撑高并发)?
下一篇:python条件判断语句