翻譯zhangv (derekzhangv.at.hotmail.com)
原文:http://www.devx.com/Java/Article/28422/0/page/3

圖3
圖3中描述了AOP方法的設計以及在一個更抽象的層次上類間的交互.你可以通過對比圖1和圖3來更好地理解AOP.
程序的目的是通過BusinessUnit對象讀取CSV文件中的記錄然后 填入類BusinessUnitService 中的map.使用AOP來填充這個map有點類似后門(backdoor)方法 -- 控制被委派給BusinessUnit 來讀取存儲介質中的記錄.
AOP就是定義一些point cut(?)和advice.一個point cut是源代碼中一個執行點.前面的例子定義了一個pointcut 給類BusinessUnitService中的findBusinessUnits 方法.一個advice就是當執行到point cut時的一塊代碼.類BusinessUnitPersistentAspect 包括advice方法findAllBusinessUnits,該方法從存儲介質中載入數據,然后使用工廠類創建BusinessUnit 對象.然后這個對象被加入map,map對象的引用通過BusinessUnitService 對象獲得.point cut 和advice組成了所謂的"方面(Aspect)"
為了讀取存儲介質中的數據,OOP方法通過一個DAO類來做.而AOP中,你在作用域類中定義一個point cut和一個advice來讀取數據.AOP框架會以advice的形式注入代碼,既可以在執行期也可以在編譯期.
總而言之,當類BusinessUnitService 中的findAllBusinessUnits 方法被調用時,AOP框架會注入advice方法并通過BusinessUnit 對象預先讀取數據來填充map對象.這樣,持久層方面的代碼可以從業務模型中移出.
新方法里的方面
本節討論如何用AOP為應用程序的各方面建模
操作資源
類BusinessUnitPersistenceAspect 的持久方法使用了一個buffered reader.你甚至可以定義方面的方面,但為了簡單,這里的討論只關注類的查找方法.
- @Aspect("perJVM")
- public class BufferedFileReaderAspect {
- @Expression("execution(* org.javatechnocrats.aop.withaop.aspects.BusinessUnitPersistenceAspect.find*(..))")
- Pointcut businessUnitPersistenceAspect;
- // 其他point cut 定義
- @Expression("businessUnitPersistenceAspect ||
- employeePersistenceAspect ||
- managerPersistenceAspect")
- Pointcut allPersistencePointcuts;
- private Map<Class, String> fileNames;
- public BufferedFileReaderAspect() {
- System.out.println("BufferedFileReaderAspect created");
- fileNames = new HashMap<Class, String>();
- fillFileNames();
- }
- @Before("allPersistencePointcuts")
- public void assignReader(JoinPoint joinPoint) throws Throwable {
- System.out.println("assignReader advice called");
- Object callee = joinPoint.getCallee();
- IBufferedFileReaderConsumable bufReaderConsumable = (IBufferedFileReaderConsumable)callee;
- Class persistenceClass = callee.getClass();
- String fileName = fileNames.get(persistenceClass);
- FileReader fileReader = new FileReader(fileName);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- bufReaderConsumable.setBufferedReader(bufferedReader);
- }
- @AfterFinally("allPersistencePointcuts")
- public void releaseReader(JoinPoint joinPoint) throws Throwable {
- //釋放buffered reader等資源
- }
- //其他方法
- }
上面的代碼試圖為每一個方法名創建一個point cut -- 所有以find開頭的方法.無論何時這些方法被調用,assignReader方法都會被提前執行.這里它獲取被調用的類實例然后設置新建的buffered reader.
同樣地,在releaseReader 方法里,代碼會預先關閉buffered reader集合.本節只解釋@before和@
AfterFinally 這兩個point cut.(以J2SE 5.0的標記定義).另外,你也可以在方面定義的xml文件中聲明他們.你可以查看那例程源代碼中的aop.xml文件.
下載
持久化
前面提到,OOP方法使用BusinessUnit 來為應用的持久層填充Map.在下面的高亮代碼中(@before一行,以及while循環代碼 - 譯者注),當BusinessUnitService 中的方法findAllBusinessUnits 被調用時advice方法findAllBusinessUnits 也將被調用.
- @Aspect("perJVM")
- public class BusinessUnitPersistenceAspect implements IBufferedFileReaderConsumable {
- private BufferedReader buffFileReader;
- @Before("execution(Collection org.javatechnocrats.aop.withaop.BusinessUnitService.findAllBusinessUnits())")
- public void findAllBusinessUnits(JoinPoint joinPoint) throws Throwable {
- System.out.println("findAllBusinessUnits advice called");
- Map<String, BusinessUnit> businessUnits =
- ((BusinessUnitService)joinPoint.getThis()).getBusinessUnits();
- String businessUnitRecord;
- while((businessUnitRecord = buffFileReader.readLine()) != null) {
- BusinessUnit businessUnit = BusinessUnitFactory.createBusinessUnit(businessUnitRecord);
- businessUnits.put(businessUnit.getId(), businessUnit);
- }
- }
- public void setBufferedReader(BufferedReader buffFileReader) {
- System.out.println("BusinessUnitPersistenceAspect.setBufferedReader called");
- this.buffFileReader = buffFileReader;
- }
- public BufferedReader getBufferedReader() {
- System.out.println("BusinessUnitPersistenceAspect.getBufferedReader called");
- return this.buffFileReader;
- }
- }
advice方法從數據存儲中讀取記錄,使用工廠類創建一個BusinessUnit實例.然后這個實例被加入到Map.該Map掌管程序的所有持久化方面.
日志/b]
本文中的例子沒有包含一個完整的日志AOP解決方案.但是,它為java.lang.Object類的toString方法定義了一個point cut來獲取類的調試信息.因此,域中的類不需要實現toString方法.通?赡苣憧赡苄枰獮槊恳粋類都要實現這個方法.
- @Aspect("perJVM")
- public class LoggingAspect {
- @Around("execution(String org.javatechnocrats.aop.withaop..*.toString())")
- public Object toStringAdvice(JoinPoint joinPoint) throws Throwable {
- System.out.println("toStringAdvice called");
- String toString = (String)joinPoint.proceed();
- Object target = joinPoint.getThis();
- Field fields[] = target.getClass().getDeclaredFields();
- List members = new ArrayList(fields.length + 1);
- members.add(toString);
- for(Field field : fields) {
- field.setAccessible(true);
- Object member = field.get(target);
- members.add(field.getName() + "=" + member);
- }
- return members.toString();
- }
你也可以用這個樣例代碼完成錯誤處理方面.
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/
關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
技術支持和業務聯系:info@testage.com.cn 電話:010-51297073
版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
技術支持和業務聯系:info@testage.com.cn 電話:010-51297073
老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月