spring 动态代理

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

import com.spring.dao.UserDao;
import com.spring.daoImpl.UserDaoImpl;
import com.spring.user.User;
 /**
  * 代理对象 和 被代理对象
  * @author nan
  *
  */
public class ProxyTest implements InvocationHandler {

	private Object targect;

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		// TODO Auto-generated method stub
		method.invoke(proxy, args);
		return null;
	}

	public void test(){
		UserDao userDao = new UserDaoImpl();//被代理对象 
		ProxyTest proxy = new ProxyTest();//代理对象
		proxy.setTargect(userDao);
		UserDao userDaoProxy = (UserDao)Proxy.newProxyInstance(userDao.getClass().getClassLoader(), new Class[]{userDao.getClass()}, proxy);
		userDaoProxy.save(new User());
	}

	public Object getTargect() {
		return targect;
	}

	public void setTargect(Object targect) {
		this.targect = targect;
	}

} 

你可能感兴趣的:(spring)