浅谈java代理

 

浅谈java代理

                 java代理,见名思意,就是代理某个java类,并执行这个java类中的方法,在Java中的主要作用是AOP编程(面向方面编程)

代理作用:

                就是在某个类的方法执行之前或者之后插入某些动作,但又不想改变代码的原始内容。

 

 

应用java代理:

要想用java代理首先熟知一个类,那就是代理类,Proxy

 

这个类中的方法都是静态的所以直接用这个Proxy来调用就行了,下面是两个重要的方法

 

getProxyClass(ClassLoader loader, Class<?>... interfaces) :

这个方法需要得到这个这个InvaocationHandle,并将这个对象作为参数来执行Proxy的newInstance来获取这个代理的类

如下所示

 

InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance()); //得到执行句柄,这个类需要覆写invoke方法
  Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});//得到带了类字节码,其中声明用那个累加器加载的这个类,一及接口
  Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);//得到代理类对象
InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
  Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
  Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
  Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
  Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
  Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
  Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);

newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h):

这个方法不过是把上面的两步变成一步操作:

InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());//得到执行句柄,这个类需要覆写invoke方法

Advice animate = (Advice) Proxy.newProxyInstance(Advice.class.getClassLoader(),new Class[]{Advice.class}, handle);//得到代理类对象

 

最后再用得到的代理对象就可以执行了

 

完整的代码如下:


生成代理的对象

public class ProxyFactoryBean {
	
	private Advice advice;
	private Object target;

	
	
	public Advice getAdvice() {
		return advice;
	}



	public void setAdvice(Advice advice) {
		this.advice = advice;
	}



	public Object getTarget() {
		return target;
	}



	public void setTarget(Object target) {
		this.target = target;
	}



	public Object getProxy() {
		// TODO Auto-generated method stub
		Object proxy = Proxy.newProxyInstance(
				target.getClass().getClassLoader(), 
				target.getClass().getInterfaces(), 
				new InvocationHandler() {
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						
						advice.doBefore();
						Object retVal = method.invoke(target, args);
						advice.doAfter();
						return retVal;
					}
				}
				);
		return proxy;
	}

	
	

}
 
获取bean
public class BeanFactory {
	
	Properties props = new Properties();
	public BeanFactory(InputStream ips){
		try {
			props.load(ips);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Object getBean(String name){
		String className = props.getProperty(name);
		Object bean = null;
		try {
			bean = Class.forName(className).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(bean instanceof ProxyFactoryBean){
			Object proxy = null;
			ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
			try {
				Advice advice = (MyAdvice) Class.forName(props.getProperty(name+".advice")).newInstance();
				Object target =  Class.forName(props.getProperty(name+".target")).newInstance();
				proxyFactoryBean.setAdvice(advice);
				proxyFactoryBean.setTarget(target);
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			proxy = proxyFactoryBean.getProxy();
			return proxy;
		}
		
		
		
		return bean;
		
	}

}

 

测试:

public class MyProxyTest {

	public static void main(String[] args) throws Throwable {
		// TODO Auto-generated method stub
		
		
		InvocationHandler handle = new MyInvocationHandle(MyAdvice.class.newInstance(),MyAdvice.class.newInstance());
		Class proxyClass = Proxy.getProxyClass(Advice.class.getClassLoader(), new Class[] {Advice.class});
		Advice animate = (Advice) proxyClass.getConstructor(InvocationHandler.class).newInstance(handle);
		//Advice animate = (Advice) Proxy.newProxyInstance(Advice.class.getClassLoader(),new Class[]{Advice.class}, handle); 

		
		animate.doBefore();
		animate.doAfter();
	
	}

}


 


 


   

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值