• 03-动态代理复习增强


    1 接口

    package www.test.service;
    
    public interface UserService {
    
        public abstract void add();
        public abstract void delete();
        public abstract void update();
        public abstract void find();
    }

    2 实现接口类的代码

    package www.test.service;
    
    public class UserServiceImpl implements UserService {
        @Override
        //添加用户
        public void add() {
            System.out.println("添加用户");
        }
        @Override
        //删除用户
        public void delete() {
            System.out.println("删除用户");
        }
        @Override
        //修改用户
        public void update() {
            System.out.println("修改用户");
        }
        @Override
        //查找用户
        public void find() {
            System.out.println("查找用户");
        }
    }

    3 获取动态代理工厂-1

    package www.test.c_proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import www.test.service.UserService;
    import www.test.service.UserServiceImpl;
    
    //1 创建一个类实现InvocationHandler
    public class UserServiceProxyFactory1 implements InvocationHandler {
        
    
        private UserService us;
        
    
        public UserServiceProxyFactory1(UserService us) {
            this.us = us;
        }
    
        //获得动态的代理对象----在运行时 在内存中动态的为 UserServiceImpl 创建一个虚拟的代理对象
        public UserService getUserServiceProxy(){
            //生成动态代理
            UserService usProxy = (UserService)Proxy.newProxyInstance( 
                    UserServiceProxyFactory1.class.getClassLoader(),
                    UserServiceImpl.class.getInterfaces(),
                    this);
            return usProxy;
        }
        
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("目标方法前的逻辑");
            Object invoke = method.invoke(us, args);
            System.out.println("目标方法前的逻辑");
            return invoke;
        }
    }

    3 获取动态代理工厂-2

    package www.test.c_proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import www.test.service.UserService;
    import www.test.service.UserServiceImpl;
    
    public class UserServiceProxyFactory2 {
    
        private UserService us;
        public UserServiceProxyFactory2(UserService us) {
            this.us = us;
        }
        //获得动态的代理对象----在运行时 在内存中动态的为 UserServiceImpl 创建一个虚拟的代理对象
        public UserService getUserServiceProxy(){
            
            UserService usProxy = (UserService) Proxy.newProxyInstance(
                    //loader :与目标对象相同的类加载器
                    UserServiceProxyFactory2.class.getClassLoader(),
                    // interfaces :代表与目标对象实现的所有接口字节码对象数组
                    UserServiceImpl.class.getInterfaces(),
                    //h: 具体的操作,InvocationHandler接口
                     new InvocationHandler() {
                        //invoke 代表的是执行代理对象的方法
                        //method: 代表目标对象的方法字节码对象
                        //args:代表目标对象的相应的方法的参数
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            System.out.println("目标方法前的逻辑");
                            //执行目标方法
                            Object invoke = method.invoke(us, args);
                            System.out.println("目标方法后的逻辑");
                            return invoke;
                        }
                    });
            return usProxy;
        }
    }

    4 获取动态代理工厂-3(简洁)

    package www.test.c_proxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import www.test.service.UserService;
    import www.test.service.UserServiceImpl;
    
    public class UserServiceProxyFactory3 {
    
        private UserService us;
        public UserServiceProxyFactory3(UserService us) {
            this.us = us;
        }
        //获得代理对象
        public UserService getUserServiceProxy(){
            
            UserService usProxy = (UserService) Proxy.newProxyInstance(
                    UserServiceProxyFactory3.class.getClassLoader(),
                    UserServiceImpl.class.getInterfaces(),
                    new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            System.out.println("目标方法前的逻辑");
                            //执行目标方法
                            Object invoke = method.invoke(us, args);
                            System.out.println("目标方法后的逻辑");
                            return invoke;
                        }
                    });
            return usProxy;
        }
    }

    5 测试

    1 UserServiceProxyFactory1 测试

    package www.test.c_proxy;
    
    import org.junit.Test;
    
    import www.test.service.UserService;
    import www.test.service.UserServiceImpl;
    
    public class Demo {
    
        @Test
        //动态代理
        public void fun1(){
            UserService us = new UserServiceImpl();
            UserServiceProxyFactory1 proxyFactory = new UserServiceProxyFactory1(us);
            UserService usProxy = proxyFactory.getUserServiceProxy();
            
            usProxy.add();
            
            //代理对象与被代理对象实现了相同的接口
            //代理对象与被代理对象没有继承关系
            System.out.println(usProxy instanceof UserServiceImpl); //false
        }
    }

    2 UserServiceProxyFactory2 测试

    package www.test.c_proxy;
    
    import org.junit.Test;
    
    import www.test.service.UserService;
    import www.test.service.UserServiceImpl;
    
    public class Demo2 {
    
        @Test
        public void fun1(){
            UserService us = new UserServiceImpl();
            UserServiceProxyFactory2 proxyFactory = new UserServiceProxyFactory2(us);
            UserService usProxy = proxyFactory.getUserServiceProxy();
            usProxy.delete();
            
            //代理对象与被代理对象实现了相同的接口
            //代理对象与被代理对象没有继承关系
            System.out.println(usProxy instanceof UserServiceImpl);//false;
        }
    }
  • 相关阅读:
    软件测试课堂练习
    JSP第一次作业
    安卓第六次作业
    安卓第五次作业
    第四次安卓作业
    JSP第四周
    软件测试课堂练习3.4
    Android数据库表
    Android购物菜单
    Android增删改查
  • 原文地址:https://www.cnblogs.com/jepson6669/p/8540665.html
Copyright © 2020-2023  润新知