jdk动态代理实现作业怎么写

来源:2-4 使用 cglib 动态产生代理

niuhao

2016-07-24 19:09

作业怎么实现有同时记录时间和日子?

写回答 关注

5回答

  • 阿__铭
    2016-12-07 14:18:02
    package com.jdkproxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    /*
     * 日志
     */
    public class LogHandler implements InvocationHandler {
    	
    	private Object target;
    
    	public LogHandler (Object target){
    		super();
    		this.target = target;
    	}
    
    	public Object getTarget() {
    		return target;
    	}
    
    	public void setTarget(Object target) {
    		this.target = target;
    	}
    
    	@Override
    	public Object invoke(Object proxy, Method method, Object[] args)
    			throws Throwable {
    		System.out.println("日志记录开始。。。");
    		method.invoke(target);
    		System.out.println("日志记录结束。。。");
    		return null;
    	}
    
    }


  • 阿__铭
    2016-12-07 14:17:02
    package com.jdkproxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Proxy;
    
    import com.proxy.Car;
    import com.proxy.Moveable;
    
    public class Text {
    
    	/**
    	 * JDK动态代理测试类
    	 * 
    	 * 动态代理实现步骤
    	 * 1.创建一个实现InvocationHandler接口的类,它必须实现invoke方法
    	 * 2.创建被代理的类和接口
    	 * 3.调用Proxy的静态方法,创建一个代理类newProxyInstance()
    	 * 4.通过代理调用方法
    	 * 
    	 */
    	public static void main(String[] args) {
    
    		Car car = new Car();
    		InvocationHandler h = new TimeHandler(car);
    		Class<?> cls= car.getClass();
    		/*
    		 * loader 类加载器
    		 * interfaces 实现接口
    		 * h InvocationHandler
    		 */
    		Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h);
    		
    		//同时实现时间和日志的动态代理
    		InvocationHandler l = new LogHandler(m);
    		Moveable m1 = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), l);
    		m1.move();
    	}
    
    }

    package com.jdkproxy;


    import java.lang.reflect.InvocationHandler;

    import java.lang.reflect.Method;


    /*

     * 日志

     */

    public class LogHandler implements InvocationHandler {

    private Object target;


    public LogHandler (Object target){

    super();

    this.target = target;

    }


    public Object getTarget() {

    return target;

    }


    public void setTarget(Object target) {

    this.target = target;

    }


    @Override

    public Object invoke(Object proxy, Method method, Object[] args)

    throws Throwable {

    System.out.println("日志记录开始。。。");

    method.invoke(target);

    System.out.println("日志记录结束。。。");

    return null;

    }


    }


  • callme激流
    2016-08-11 12:09:50
    public class Test {
    	public static void main(String[] args) {
    		Car car = new Car();
    		//timeHandler是添加了time的car代理
    		InvocationHandler timeHandler=  new TimeProxy(car);
    		Class<?> cls = car.getClass();
    		
    		Move timemove = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), timeHandler);
    		//注意此处timemove代替car,logHandler则为添加了time和log的car代理
    		InvocationHandler logHandler =  new LogProxy(timemove);
    		
    		/**
    		 * loader:类加载器,
    		 * interfaces:实现接口
    		 * h:InvocationHandler
    		 */
    		
    		Move logmove = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), logHandler);
    		logmove.move();
    	}
    }


  • callme激流
    2016-08-11 11:51:32
    public static void main(String[] args) {
    		Car car = new Car();
    		InvocationHandler h =  new JDKProxy(car);
    		Class<?> cls = car.getClass();
    		Move m = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h);
    		InvocationHandler h1 =  new LogProxy(m);
    		
    		/**
    		 * loader:类加载器,
    		 * interfaces:实现接口
    		 * h:InvocationHandler
    		 */
    		
    		Move m1 = (Move) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h1);
    		m1.move();
    	}


  • 孜然香香
    2016-07-25 11:49:08

    把相关信息都加一个里面

模式的秘密---代理模式

本节课程将带你领略Java编程语言中代理模式的奥妙

54906 学习 · 125 问题

查看课程

相似问题