Java实现AOP功能的封装与配置的小框架实例代码
程序员文章站
2023-11-21 13:26:34
本文通过是动态代理实现的aop功能的封装与配置的小框架.加深对动态代理和aop编程的理解
设计
根据配置文件的键xxx对应的值(类全名)创建相应类的对象。
当且仅当x...
本文通过是动态代理实现的aop功能的封装与配置的小框架.加深对动态代理和aop编程的理解
设计
根据配置文件的键xxx对应的值(类全名)创建相应类的对象。
当且仅当xxx对应的值为com.iot.proxy.aopframework.proxyfactorybean
时,则生成相应的动态代理类对象。代理对象的目标类和通知实现类分别由xxx.target
和xxx.advice
配置
配置文件
config.propertiest
位于aopframework包下
- xxx代表要加载的类
- xxx.advice代表通知接口的某个实现类
- xxx.target代表委托类
#xxx=java.util.arraylist
xxx=com.iot.proxy.aopframework.proxyfactorybean
xxx.advice=com.iot.proxy.myadvice
xxx.target=java.util.arraylist
包:com.iot.proxy.aopframework
,有如下几个类/接口:
- beanfactory,用于读取配置文件,根据配置创建相应的对象
- proxyfactorybean,用于生成代理对象,含有两个私有属性:目标和通知
- advice,通知接口,用于把切面的代码以对象的形式传递给invocationhandler的的invoke方法
- myadvice,advice接口的一个实现类,打印执行方法前的时间及执行耗时
- aopframeworktest,测试效果
代码
advice接口
package com.iot.proxy.aopframework; import java.lang.reflect.method; /** * created by brian on 2016/2/2. */ public interface advice { void beforemethod(method method); void afteremethod(method method); }
myadvice类
package com.iot.proxy.aopframework; import java.lang.reflect.method; /** * created by brian on 2016/2/2. */ public class myadvice implements advice{ long begintime = 0 ; @override public void beforemethod(method method) { system.out.println(method.getname()+" before at "+begintime); begintime = system.currenttimemillis(); } @override public void afteremethod(method method) { long endtime = system.currenttimemillis(); system.out.println(method.getname()+" cost total "+ (endtime-begintime)); } }
beanfactory类
package com.iot.proxy.aopframework; import java.io.ioexception; import java.io.inputstream; import java.util.properties; /** * created by brian on 2016/2/2. */ public class beanfactory { properties properties = new properties(); public beanfactory(inputstream inputstream){ try { properties.load(inputstream); } catch (ioexception e) { e.printstacktrace(); } } public object getbean(string name){ string classname = properties.getproperty(name); object bean = null; try { class clazz = class.forname(classname); bean = clazz.newinstance(); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (instantiationexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } if (bean instanceof proxyfactorybean){ proxyfactorybean proxyfactorybean = (proxyfactorybean)bean; advice advice = null; object target = null; try { advice = (advice) class.forname(properties.getproperty(name+".advice")).newinstance(); target = class.forname(properties.getproperty(name+".target")).newinstance(); } catch (instantiationexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } proxyfactorybean.setadvice(advice); proxyfactorybean.settarget(target); object proxy = ((proxyfactorybean) bean).getproxy(); return proxy; } return bean; } }
proxyfactorybean类
package com.iot.proxy.aopframework; import java.lang.reflect.invocationhandler; import java.lang.reflect.method; import java.lang.reflect.proxy; /** * created by brian on 2016/2/3. */ public class proxyfactorybean { private object target; private advice advice; public object getproxy(){ 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.beforemethod(method); object retval = method.invoke(target,args); advice.afteremethod(method); 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; } }
aopframeworktest类
package com.iot.proxy.aopframework; import java.io.inputstream; import java.util.collection; /** * created by brian on 2016/2/3. */ public class aopframeworktest { public static void main(string[] args) { inputstream inputstream = aopframeworktest.class.getresourceasstream("config.properties"); object bean = new beanfactory(inputstream).getbean("xxx"); system.out.println(bean.getclass().getname()); ((collection) bean).clear(); } }
输出
- 配置xxx=com.iot.proxy.aopframework.proxyfactorybean
输出为:
com.sun.proxy.$proxy0
clear before at 0
clear cost total 0
- 配置xxx=java.util.arraylist
输出为:
java.util.arraylist
可以看出,只改变配置文件,就可改变代码的运行结果,从而达到灵活的效果
总结
以上就是本文关于java实现aop功能的封装与配置的小框架实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!