动态代理的实现3-实现aop功能的封装和配置

//一个通告或建议的接口
public interface Advice {
	void afterMethod(Method method);
	void beforeMethod(Method method);
}
public class MyAdvice implements Advice{
	private long start;
	@Override
	public void afterMethod(Method method) {
		System.out.println("运行时间计算结束!!");
		System.out.println("共耗时:"+(System.currentTimeMillis()-start));
		System.out.println("------------------------");
	}

	@Override
	public void beforeMethod(Method method) {
		System.out.println("开始计算运行时间:");
		start=System.currentTimeMillis();
		System.out.println("所执行的方法:"+method.getName());
	}
}
Properties文件的配置
#xxx=java.util.ArrayList
xxx=aopframework.ProxyFactoryBean
xxx.advice=aopframework.MyAdvice
xxx.target=java.util.ArrayList
/**
 * 负责创建目标类或代理类的实例对象,并通过配置文件实现切换
 *
 */
public class BeanFactory {
	private Properties prop;
	public BeanFactory(InputStream is){
		prop=new Properties();
		try {prop.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public Object getBean(String propkey){
		String className=prop.getProperty(propkey);
		Object bean=null;
		try {
			bean=Class.forName(className).newInstance();
			if(bean instanceof ProxyFactoryBean){
				String adviceName=prop.getProperty(propkey+".advice");
				String targetName=prop.getProperty(propkey+".target");
				try {
					Advice advice = (Advice) Class.forName(adviceName).newInstance();
					Object target = Class.forName(targetName).newInstance();
					ProxyFactoryBean beanproxy=(ProxyFactoryBean)bean;
					beanproxy.setAdvice(advice);
					beanproxy.setTarget(target);
					bean=beanproxy.getProxy();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
	}
}
/**
 * 充当封装生成动态代理的工厂
 */
public class ProxyFactoryBean {
	private Advice advice;
	private Object target;
	public Object getProxy() {
		Object coll=(Object) Proxy.newProxyInstance(
				target.getClass().getClassLoader(), 
				target.getClass().getInterfaces(), 
				new InvocationHandler(){
					@Override
					public Object invoke(Object proxy, Method method,
							Object[] args) throws Throwable {
						advice.beforeMethod(method);
						Object obj=method.invoke(target, args);
						advice.afterMethod(method);
						return obj;
					}
				});
		return coll;
	}
	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 class Test {
	public static void main(String[] args) {
		InputStream is=Test.class.getClassLoader().getResourceAsStream("config.properties");
		BeanFactory factory=new BeanFactory(is);
		Collection coll=(Collection) factory.getBean("xxx");
		coll.add("aaa");
		coll.add("222");
		System.out.println(coll.size());
	}
	/**
	 * 运行结果
	           开始计算运行时间:
		所执行的方法:add
		运行时间计算结束!!
		共耗时:0
		------------------------
		开始计算运行时间:
		所执行的方法:add
		运行时间计算结束!!
		共耗时:0
		------------------------
		开始计算运行时间:
		所执行的方法:size
		运行时间计算结束!!
		共耗时:0
		------------------------
		2
	 */
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值