通过反射模拟AOP的动态代理

config.property


#xxx=java.util.ArrayList
xxx=AOPProxy.ProxyFactoryBean
xxx.advice=AOPProxy.Advice
xxx.target=java.util.ArrayList

BeanFactory


package AOPProxy;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class BeanFactory {
	Properties pro = new Properties();
	public BeanFactory(InputStream is) throws IOException{
		pro.load(is);
	}
	public Object getBean(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		Object proxy = null;
		String className = pro.getProperty(name);
		Class cla = Class.forName(className);
		Object bean = (Object)cla.newInstance();
		System.out.println("-----------------");
		
		if(bean instanceof ProxyFactoryBean){
			Advice advice =(Advice) Class.forName((String)pro.get(name +".advice")).newInstance();
			Object target = (Object)Class.forName((String)pro.get(name +".target")).newInstance();
			
			ProxyFactoryBean proxyFactoryBean = (ProxyFactoryBean)bean;
			proxyFactoryBean.setAdvice(advice);
			proxyFactoryBean.setTarget(target);
			proxy = (proxyFactoryBean).getProxy();
			return proxy;
		}
		return bean;
	}
	private Object ProxyFactoryBean(Object bean) {
		// TODO Auto-generated method stub
		return null;
	}
}




ProxyFactoryBean

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


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 proxy3 = (Object)Proxy.newProxyInstance(
				target.getClass().getClassLoader(),
				//new Class[]{Collection.class},
				target.getClass().getInterfaces(),
				new InvocationHandler() {
					//分别是对象,方法,参数
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
/*						long bt = System.currentTimeMillis();
						//将调用的方法关联起来
						Object retVal = method.invoke(target, args);
						for (int i = 0; i <10; i++) {
						}
						long et = System.currentTimeMillis();
						System.out.println(method.getName()+" "+ (et - bt )+"hehe");
						return retVal;*/
						
						
						//直接调用方法
						advice.beforeMethod(method);
						//将调用的方法关联起来
						Object retVal = method.invoke(target, args);
						for (int i = 0; i <10; i++) {
						}
						advice.afterMethod(method);
						return retVal;
					}
				}
		);
		return proxy3;

	}

}


AOPTest


package AOPProxy;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class AopTest {
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
		InputStream is = (InputStream)AopTest.class.getResourceAsStream("config.properties");
		Object bean = (Object)new BeanFactory(is).getBean("xxx");
		System.out.println(bean.getClass().getName());
		//System.out.println(bean.getClass().getName());
	}
}


Advice:


package AOPProxy;
import java.lang.reflect.Method;


public class Advice {
	long bt,et;
	public void beforeMethod(Method method) {
		// TODO Auto-generated method stub
		bt = System.currentTimeMillis();
	}

	public void afterMethod(Method method) {
		// TODO Auto-generated method stub
		et = System.currentTimeMillis();
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值