zoukankan      html  css  js  c++  java
  • 代理模式

    静态代理

    package com.bjsxt.proxy;
    
    import com.bjsxt.service.SomeService;
    
    //静态代理类,要和目标类实现相同接口
    public class ServiceProxy implements SomeService {
        SomeService target;
        
        public ServiceProxy() {
            super();
        }
        
    
        public ServiceProxy(SomeService target) {
            super();
            this.target = target;
        }
    
    
        public String doSome(){
            return target.doSome().toUpperCase();
        }
    }
    View Code
    package com.bjsxt.service;
    
    //主业务接口
    public interface SomeService {
        String doSome();
    }
    View Code
    package com.bjsxt.service.impl;
    
    import com.bjsxt.service.SomeService;
    
    //目标类
    public class SomeServiceImpl implements SomeService {
        
        
        public SomeServiceImpl() {
            System.out.println("无参构造器执行!");
        }
    
        @Override
        public String doSome() {
            return "China";
        }
    
    }
    View Code
    package com.bjsxt.test;
    
    import com.bjsxt.proxy.ServiceProxy;
    import com.bjsxt.service.SomeService;
    import com.bjsxt.service.impl.SomeServiceImpl;
    
    public class SomeTest {
        public static void main(String[] args) {
            //定义目标对象
            SomeService target = new SomeServiceImpl();
            //定义目标对象的代理对象
            SomeService proxy = new ServiceProxy(target);
            String result = proxy.doSome();
            System.out.println(result);
        }
    }
    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- bean的定义:以下配置相当于SomeService service = new SomeServiceImpl(); -->
        <bean id="someServiceImpl" class="com.bjsxt.service.impl.SomeServiceImpl"></bean>
    </beans>
    View Code

    动态代理

    分为:jdk动态代理和CGLIB动态代理

    package com.bjsxt.test;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import com.bjsxt.service.SomeService;
    import com.bjsxt.service.impl.SomeServiceImpl;
    
    public class SomeTest {
        public static void main(String[] args) {
            //定义目标对象
            final SomeService target = new SomeServiceImpl();
            //定义目标对象的代理对象
            SomeService proxy = (SomeService) Proxy.newProxyInstance(target.getClass().getClassLoader(),//目标类的类加载器
                                                        target.getClass().getInterfaces(),//目标类实现的所有接口
                                                        new InvocationHandler() {//调用处理器
                                                            //proxy:代理对象
                                                            //method:目标方法
                                                            //args:目标方法参数
                                                            @Override
                                                            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                                                                String result = (String) method.invoke(target, args);
                                                                return result.toUpperCase();
                                                            }
                                                        });
            String result1 = proxy.doSome();
            System.out.println(result1);
        }
    }
    View Code
  • 相关阅读:
    Java Set 常用集合 HashSet、LinkedHashSet、TreeSet
    旋转数组的最小数字
    Java List 常用集合 ArrayList、LinkedList、Vector
    RestfulApi 学习笔记——内容协商(三)
    RestfulApi 学习笔记——.net core入门操作(二)
    不一样的模板模式(设计模式十一)
    RestfulApi 学习笔记——简单介绍(一)
    oracle 数据库连接
    重学c#系列——索引器(九)
    重新整理计算机组成原理(一)
  • 原文地址:https://www.cnblogs.com/wq-9/p/10154354.html
Copyright © 2011-2022 走看看