实现类似spring的可配置的AOP框架




package again.aopframework;

import java.io.InputStream;
import java.util.Collection;

public class AopFrameworkTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		/*这个总程序的思想是:
		如果xxx是java.util.ArrayList  就直接返回其实例
		如果xxx是again.aopframework.MyAdvice就返回其java.util.ArrayList的代理类*/
		
		InputStream ips=AopFrameworkTest.class.getResourceAsStream("config.properties");
		
		Object obj=new BeanFactory(ips).getBean("xxx");
		
		System.out.println(obj.getClass().getName());
		
		Collection col=(Collection)obj;
		col.add("ffff");
		System.out.println(col.size());
		
	}

}
|
package again.aopframework;

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

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 {
			
			Class clazz=Class.forName(className);
			bean=clazz.newInstance();//JavaBean  必须要有一个不带参数的构造方法
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		if(bean instanceof ProxyFactoryBean){
			Object proxy=null;
			ProxyFactoryBean proxyFactoryBean=(ProxyFactoryBean)bean;
			try{
				Advice advice=(Advice)Class.forName(props.getProperty(name+".advice")).newInstance();
				Object target=Class.forName(props.getProperty(name+".target")).newInstance();
				proxyFactoryBean.setAdvice(advice);
				proxyFactoryBean.setTarget(target);
				proxy=proxyFactoryBean.getProxy(target,advice);
			}
			catch(Exception e){
				e.printStackTrace();
			}
			return proxy;
		}
		
		return bean;
	}
}
|
package again.aopframework;

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

class ProxyFactoryBean {
	private Object target;
	private Advice advice;
	public Object getProxy(final Object target,final Advice advice){
		Object proxy=Proxy.newProxyInstance(
				target.getClass().getClassLoader(),
				target.getClass().getInterfaces(),
				new InvocationHandler(){

					@Override
					public Object invoke(Object arg0, Method arg1, Object[] arg2)
							throws Throwable {
						// TODO Auto-generated method stub
						advice.beforeMethod(arg1);
						Object retVal=arg1.invoke(target, arg2);
						advice.afterMethod(arg1);
						return retVal;
					}
					
				}
				);
		return proxy;
	}
	
	
	
	public Object getTarget() {
		return target;
	}
	public void setTarget(Object target) {
		this.target = target;
	}
	public Advice getAdvice() {
		return advice;
	}
	public void setAdvice(Advice advice) {
		this.advice = advice;
	}

	
	
}
|
package again.aopframework;

import java.lang.reflect.Method;
class MyAdvice implements Advice {
	long beginTime;
	@Override
	public void beforeMethod(Method method) {
		// TODO Auto-generated method stub
		beginTime=System.currentTimeMillis();
	}

	@Override
	public void afterMethod(Method method) {
		// TODO Auto-generated method stub
		long endTime=System.currentTimeMillis();
		System.out.println("耗时:"+(endTime-beginTime));
	}

}
|
package again.aopframework;

import java.lang.reflect.Method;

interface Advice {
	void beforeMethod(Method method);
	void afterMethod(Method method);
}
|
#xxx=java.util.ArrayList
xxx=again.aopframework.ProxyFactoryBean
xxx.advice=again.aopframework.MyAdvice
xxx.target=java.util.ArrayList




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值