spring中的代理

缺乏、安全感 2023-01-24 05:20 171阅读 0赞

1.静态代理:

  1. public class Main2 {
  2. //这里传入的是接口类型的对象,方便向上转型,实现多态
  3. public static void consumer(ProxyInterface pi){
  4. pi.say();
  5. }
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8. consumer(new ProxyObject());
  9. }
  10. }
  11. //代理接口
  12. interface ProxyInterface{
  13. public void say();
  14. }
  15. //被代理者
  16. class RealObject implements ProxyInterface{
  17. //实现接口方法
  18. @Override
  19. public void say() {
  20. // TODO Auto-generated method stub
  21. System.out.println("say");
  22. }
  23. }
  24. //代理者
  25. class ProxyObject implements ProxyInterface{
  26. @Override
  27. public void say() {
  28. // TODO Auto-generated method stub
  29. //dosomething for example
  30. System.out.println("hello proxy");
  31. new RealObject().say();
  32. System.out.println("this is method end");
  33. }
  34. }
  35. hello proxy
  36. say
  37. this is method end

控制台输出:

hello proxy
say
this is method end

2.动态代理:

  1. package docker.demo;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. /**
  6. * @program:dockerdemo
  7. * @desc:
  8. * @author:qiyihang
  9. * @date:2020/12/15
  10. */
  11. public class DynamicProxy {
  12. static void customer(ProxyInterface pi){
  13. pi.say();
  14. }
  15. public static void main(String[] args){
  16. RealObject real = new RealObject();
  17. ProxyInterface proxy = (ProxyInterface) Proxy.newProxyInstance(ProxyInterface.class.getClassLoader(),new Class[]{ProxyInterface.class}, new ProxyObject(real));
  18. customer(proxy);
  19. }
  20. }
  21. interface ProxyInterface{
  22. void say();
  23. }
  24. //被代理类
  25. class RealObject implements ProxyInterface{
  26. public void say(){
  27. System.out.println("i'm talking");
  28. }
  29. }
  30. //代理类,实现InvocationHandler 接口
  31. class ProxyObject implements InvocationHandler {
  32. private Object proxied = null;
  33. public ProxyObject(){
  34. }
  35. public ProxyObject(Object proxied){
  36. this.proxied = proxied;
  37. }
  38. public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
  39. System.out.println("hello");
  40. return arg1.invoke(proxied, arg2);
  41. };
  42. }

控制台输出:

hello
i’m talking

代理类实现了InvocationHandler接口,

  1. ProxyInterface proxy = (ProxyInterface) Proxy.newProxyInstance(ProxyInterface.class.getClassLoader(),new Class[]{ProxyInterface.class}, new ProxyObject(real));

得到接口的实例,作为参数传递到customer(),这里每一个在代理类上处理的东西也会被重定向到调用处理器上。

Proxy.newProxyInstance()方法,接收三个参数:第一个参数指定当前目标对象使用的类加载器,获取加载器的方法是固定的;第二个参数指定目标对象实现的接口的类型;第三个参数指定动态处理器,执行目标对象的方法时,会触发事件处理器的方法。


源码分析之后补上,先做个记录
摘抄自: Java动态代理与反射详解

发表评论

表情:
评论列表 (有 0 条评论,171人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring代理Proxy

    什么代理模式? 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后处理消息等

    相关 Spring代理

    概述: 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理访问目标对象。这样好处是可以在目标对象实现的基础上,增强额外的功能操作(扩展目标对象的