java基础知识(一) JDK动态代理

一、简单说明
使用java动态代理可以使得面向切面编程变得容易,也可以轻松屏蔽掉一些被代理对象的实现细节。中JDK态代理只能代理一个或者多个接口,因此使用此种方法被代理的类对象一定是实现了一个接口的。
二、相关的类和方法
java.lang.reflect.ProxyProxy
提供用于创建动态代理类和实例的静态方法:
Static Object newProxyInstan(ClassLoaderloader, Class[] interfaces, InvocationHandler h)
该方法返回代理类的一个实例,返回后的代理类可以当作被代理类使用,与代理对象关联,当请求分发到代理对象后,会自动执行h.invoke()方法, invoke法就是我们用来做N多事情的地方其中loaderinterfaces的获取方式有两种,以loader为例,一种是对象.getClass().getClassLoader(),一种是.class.getClassLoader()
java.lang.reflect.InvocationHandlerInvocationHandler 
是代理实例的调用处理程序实现的接口。处理程序在实现InvocationHandler接口时只需要实现一个方法:  
Object invoke(Object proxy, Method method, Object[] args)
其中的proxy是代理对象,method是被代理对象的方法,后面会调用method.invoke(target,args),args是方法的参数列表。
三、具体应用
动态代理的原理图如下所示:
图 1. 代理模式
下面通过新建一个工程来说明使用上述方法可以实现动态代理多个实现了接口的类,且代理类扩展了委托类的多个方法,在原方法的基础上,将日志输出到原始的输出结果前后。 建立的工程如图所示:
其中各个文件的源代码如下:
package com.myproxy.test;

public interface UserDaoImpl {
	public Boolean findUserByName(String name);
}
package com.myproxy.test;

public class UserDao implements UserDaoImpl{

	@Override
	public Boolean findUserByName(String name) {
		// TODO Auto-generated method stub
		if(name.equals("张无忌")) {
			System.out.println("查找姓名为" + name + "的用户信息成功!");
			return true;
		} else {
			System.out.println("查找姓名为" + name + "的用户信息失败!");
			return false;
		}
	}

}
package com.myproxy.test;

public interface DocumentDaoImpl{
	public Boolean findDocumentById(int id);
}
package com.myproxy.test;

public class DocumentDao implements DocumentDaoImpl{

	@Override
	public Boolean findDocumentById(int id) {
		// TODO Auto-generated method stub
		if(id == 1001) {
			System.out.println("查找id为" + id + "的文档信息成功!");
			return true;
		} else {
			System.out.println("查找id为" + id + "的文档信息失败!");  
            return false;  
		}
		
	}

}
package com.myproxy.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class DaoHandler implements InvocationHandler{
	private Calendar calendar;
	private Object target;
	
	public DaoHandler(Object target){
		this.target = target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		// TODO Auto-generated method stub
		Object result = null;
		beforeInvoke();
		result = method.invoke(target, args);
		afterInvoke();
		return result;
	}
	
	public void beforeInvoke() {
		calendar = new GregorianCalendar();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);  
        int minute = calendar.get(Calendar.MINUTE);  
        int second = calendar.get(Calendar.SECOND); 
        String time = hour + ":" + minute + ":" + second;
        System.out.println("调用时间:" + time);
	}
	
	public void afterInvoke() {
		System.out.println("方法调用结束!" );  
	}
}
package com.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

import com.myproxy.test.*;

public class ProxyTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DaoHandler handler = null;
		
		UserDao user = new UserDao();
		handler = new DaoHandler(user);
		UserDaoImpl userProxy = (UserDaoImpl)Proxy.newProxyInstance
				(user.getClass().getClassLoader(), user.getClass().getInterfaces(), handler);
		userProxy.findUserByName("张无忌");
		userProxy.findUserByName("令狐冲");
		System.out.println("-----------------");
		
		DocumentDaoImpl doc = new DocumentDao();
		handler = new DaoHandler(doc);
		DocumentDaoImpl docProxy = (DocumentDaoImpl)Proxy.newProxyInstance
				(doc.getClass().getClassLoader(), doc.getClass().getInterfaces(), handler);
		docProxy.findDocumentById(1001);
		docProxy.findDocumentById(1002);
	}

}
输出结果如下:
四、经验教训
中间出错的地方是在DaoHandler的构造函数处,将
this.target = target
写成了
target = this.target
从而 使 target 的值为null,抛出了 NUllPointerException异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值