Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼
本文通過是動(dòng)態(tài)代理實(shí)現(xiàn)的AOP功能的封裝與配置的小框架.加深對(duì)動(dòng)態(tài)代理和AOP編程的理解
設(shè)計(jì)
根據(jù)配置文件的鍵xxx對(duì)應(yīng)的值(類全名)創(chuàng)建相應(yīng)類的對(duì)象。
當(dāng)且僅當(dāng)xxx對(duì)應(yīng)的值為com.iot.proxy.aopframework.ProxyFactoryBean
時(shí),則生成相應(yīng)的動(dòng)態(tài)代理類對(duì)象。代理對(duì)象的目標(biāo)類和通知實(shí)現(xiàn)類分別由xxx.target
和xxx.advice
配置
配置文件
config.propertiest
位于aopframework包下
- xxx代表要加載的類
- xxx.advice代表通知接口的某個(gè)實(shí)現(xiàn)類
- 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
,有如下幾個(gè)類/接口:
- BeanFactory,用于讀取配置文件,根據(jù)配置創(chuàng)建相應(yīng)的對(duì)象
- ProxyFactoryBean,用于生成代理對(duì)象,含有兩個(gè)私有屬性:目標(biāo)和通知
- Advice,通知接口,用于把切面的代碼以對(duì)象的形式傳遞給InvocationHandler的的invoke方法
- MyAdvice,Advice接口的一個(gè)實(shí)現(xiàn)類,打印執(zhí)行方法前的時(shí)間及執(zhí)行耗時(shí)
- 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
可以看出,只改變配置文件,就可改變代碼的運(yùn)行結(jié)果,從而達(dá)到靈活的效果
總結(jié)
以上就是本文關(guān)于Java實(shí)現(xiàn)AOP功能的封裝與配置的小框架實(shí)例代碼的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
java -jar設(shè)置添加啟動(dòng)參數(shù)實(shí)現(xiàn)方法
這篇文章主要介紹了java -jar設(shè)置添加啟動(dòng)參數(shù)實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02IDEA搭建Maven模塊化項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了IDEA搭建Maven模塊化項(xiàng)目的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05JDK8 new ReentrantLock((true)加鎖流程
這篇文章主要介紹了java面試中常遇到的問題JDK8 new ReentrantLock((true)加鎖流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07springboot配置內(nèi)存數(shù)據(jù)庫H2教程詳解
這篇文章主要介紹了springboot配置內(nèi)存數(shù)據(jù)庫H2的詳細(xì)教程,需要的朋友可以參考下2017-07-07Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列
這篇文章主要介紹了Spring?Boot?使用?Disruptor?做內(nèi)部高性能消息隊(duì)列,工作中遇到項(xiàng)目使用Disruptor做消息隊(duì)列,對(duì)你沒看錯(cuò),不是Kafka,也不是rabbitmq。Disruptor有個(gè)最大的優(yōu)點(diǎn)就是快,還有一點(diǎn)它是開源的哦,下面做個(gè)簡單的記錄2022-06-06