将AspectJ集成到基于Eclipse + Lomboz + XmlBuddy的Web应用中去 - 基础篇
2008-01-05 19:59:37 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閻愵剙鍔ょ紓宥咃躬瀵鎮㈤崗灏栨嫽闁诲酣娼ф竟濠偽i鍓х<闁绘劦鍓欓崝銈囩磽瀹ュ拑韬€殿喖顭烽幃銏ゅ礂鐏忔牗瀚介梺璇查叄濞佳勭珶婵犲伣锝夘敊閸撗咃紲闂佺粯鍔﹂崜娆撳礉閵堝洨纾界€广儱鎷戦煬顒傗偓娈垮枛椤兘骞冮姀銈呯閻忓繑鐗楃€氫粙姊虹拠鏌ュ弰婵炰匠鍕彾濠电姴浼i敐澶樻晩闁告挆鍜冪床闂備胶绮崝锕傚礈濞嗘挸绀夐柕鍫濇川绾剧晫鈧箍鍎遍幏鎴︾叕椤掑倵鍋撳▓鍨灈妞ゎ厾鍏橀獮鍐閵堝懐顦ч柣蹇撶箲閻楁鈧矮绮欏铏规嫚閺屻儱寮板┑鐐板尃閸曨厾褰炬繝鐢靛Т娴硷綁鏁愭径妯绘櫓闂佸憡鎸嗛崪鍐簥闂傚倷娴囬鏍垂鎼淬劌绀冮柨婵嗘閻﹂亶姊婚崒娆掑厡妞ゃ垹锕ら埢宥夊即閵忕姷顔夐梺鎼炲労閸撴瑩鎮橀幎鑺ョ厸闁告劑鍔庢晶鏇犵磼閳ь剟宕橀埞澶哥盎闂婎偄娲ゅù鐑剿囬敃鈧湁婵犲﹤鐗忛悾娲煛鐏炶濡奸柍瑙勫灴瀹曞崬鈻庤箛鎾寸槗缂傚倸鍊烽梽宥夊礉鎼达絽鍨濇い鏍仜妗呴梺鍛婃处閸ㄦ壆绮婚幎鑺ュ€甸柨婵嗙凹缁ㄨ棄霉閻樿崵鐣烘慨濠冩そ濡啫鈽夊▎鎰€烽梺璇插閻噣宕¢幎鑺ュ仒妞ゆ洍鍋撶€规洖鐖奸、妤佸緞鐎n偅鐝┑鐘愁問閸n垳寰婇崜褉鍋撶粭娑樻搐缁犳煡鏌涢妷顔煎闁藉啰鍠栭弻锝夊棘閹稿孩鍠愰梺鑽ゅ枎缂嶅﹪寮诲☉鈶┾偓锕傚箣濠靛洨浜俊鐐€ら崜娆撴偋閸℃稈鈧棃宕橀鍢壯囧箹缁厜鍋撻懠顒€鍤紓鍌氬€风欢锟犲窗濡ゅ懎绠伴柟闂寸劍閸嬧晠鏌i幋锝嗩棄缁绢厸鍋撻梻浣虹帛閸旀洜绮旈棃娴虫盯宕橀鍏兼К闂侀€炲苯澧柕鍥у楠炴帡骞嬪┑鎰磻闁诲氦顫夐幐椋庣矆娓氣偓閸╃偤骞嬮敂钘変汗闂佸湱绮敮鈺傚閳ь剛绱撴担鐟板姢鐟滄壆鍋熼崚鎺戔枎閹惧疇鎽曞┑鐐村灦閻喖鈻介鍫熺厵閻熸瑥瀚慨鍥ㄣ亜閵夛妇绠炴慨濠冩そ閺屽懘鎮欓懠璺侯伃婵犫拃鍌氬祮闁哄瞼鍠栭幖褰掝敃閿濆懐锛撻梻浣瑰缁诲嫰宕戝☉銏犵厴闁瑰濮崑鎾绘晲鎼存ê浜炬い鎾寸⊕濞呭﹪鏌$仦鐣屝f繛纰变邯楠炲繒浠﹂挊澶婅厫闂傚倷鐒﹂惇褰掑磹閺囥垹绠犻柟閭﹀枟椤洟鏌熼幆褏鎽犲┑顖涙尦閺屾盯骞橀弶鎴犵シ闂佸憡鎸稿畷顒勨€旈崘顔嘉ч柛鈩冾殘娴犳悂姊洪懡銈呮毐闁哄懏鐩幃楣冩倻閽樺)銊ф喐婢舵劕纾婚柟鍓х帛閺呮煡骞栫划鐟板⒉闁诲繐绉瑰铏圭磼濡闉嶅┑鐐插级閿曘垺淇婇悽绋跨妞ゆ牗姘ㄩ悿鈧梻鍌氬€搁悧濠勭矙閹邦喛濮抽柤娴嬫櫇绾捐棄霉閿濆牊顥夐柣鎾村姈閹便劌螣缁嬪灝顬嬪┑鈥冲级閸旀瑩鐛Ο鍏煎珰闁肩⒈鍓﹀Σ浼存⒒娴gǹ鏆遍柟纰卞亰瀹曟劖绻濆В绋挎喘瀵埖鎯旈幘瀛樻澑婵$偑鍊栧濠氬Υ鐎n亶鍟呴柕澶涜礋娴滄粍銇勯幘璺轰粶婵℃彃顭烽弻锝夋晲閸パ冨箣濡ょ姷鍋炵敮锟犵嵁鐎n喖绫嶉柍褜鍓熼幃妤佺節濮橆厸鎷洪柣鐔哥懃鐎氼參宕曞Δ鍛厱婵☆垵銆€閸嬫捇鎮㈤幓鎺戠阀濠电姷鏁告慨鐑藉极閸涘﹥鍙忛柣鎴f閺嬩線鏌涘☉姗堟敾闁告瑥绻戦妵鍕箻閸楃偟浠肩紒鐐劤椤兘寮婚悢鐓庣鐟滃繒鏁☉銏$厓闂佸灝顑呴悘锕傛煏閸パ冾伃妤犵偞甯″畷鍗烆渻閹屾缂傚倸鍊搁崐椋庣矆娓氣偓钘濋梺顒€绉撮弸浣糕攽閻樿櫕鐨戠€规挷绶氶弻娑㈠焺閸愵亖濮囬梺绋匡功閸忔﹢寮诲☉妯锋斀闁糕剝顨忔导鈧俊鐐€栧褰掑礉閺囥垹鐓橀柟杈鹃檮閸婂鏌涢妷銏℃珖閺嶏繝姊绘担鍛婂暈闁圭ǹ顭烽幃鐑芥晜閻e备鏀虫繝鐢靛Т濞诧箓宕甸崘顔界厓闁告繂瀚弳鐔兼煥濞戞瑧鐭掓慨濠囩細閵囨劙骞掗幋婊冩瀳闂備礁鎲¢悷銉︻殽閹间礁鐓濋柟鐐灱閸亪鏌涢銈呮灁闁告ɑ鎮傞弻锝堢疀閺囩偘鎴风紒缁㈠幖閻栫厧鐣烽幋锕€绠婚悹鍥皺閻も偓濠电偠鎻徊浠嬪箟閿熺姴纾规い鏍仦閳锋垹鐥鐐村櫣濞存粌缍婇幃璺衡槈閺嵮冨Е闂佺硶鏂侀崑鎾愁渻閵堝棗绗掗柛鐕佸亰閹啫煤椤忓懐鍘告繛杈剧到濠€杈ㄦ櫠椤忓牊鐓冮悷娆忓閻忔挳鏌熼鐣屾噰鐎殿喖鐖奸獮瀣偐鏉堫煈鏁囬梻鍌氬€风粈浣革耿鏉堛劎浠氶梻浣侯攰婵倗鍒掓惔銊ョ闁圭儤顨呯猾宥夋煕椤愩倕鏋庡ù鐘烘缁辨挻鎷呴崜鎻掑壍濡炪倖娲樻繛濠囧极閸愵喖纾兼繛鎴炶壘楠炲牓姊绘担鍛婃儓婵炲眰鍨藉畷婵嗙暆閸曨剙鈧爼鏌eΟ鑲╁笡闁绘挻娲熼弻鐔兼嚋椤掆偓婵$厧霉濠婂嫬鍔ら柍瑙勫灴閺佸秹宕熼鈩冩線闂備胶枪閿曘儵鎮ч悩鑼殾婵犻潧顑嗛弲婵嬫煃瑜滈崜鐔煎灳閿曞倸閿ゆ俊銈傚亾闁绘帒鐏氶妵鍕箳瀹ュ牆鍘$紓浣哄Т婢т粙鍩€椤掆偓閸樻粓宕戦幘鏂ユ斀闁绘ǹ浜粣鏃堟煕鐎n偒娈旈柍瑙勫灴椤㈡瑧娑甸悜鐣屽弽婵犵數鍋涢幏鎴犲緤閸啣锝夊箛閺夎法顔婇梺鐟板暱绾绢參宕伴幘璇茬闁绘ḿ绮崵鎴︽煠缁嬭法浠涙慨锝嗗姍濮婂宕掑顑藉亾閻戣姤鍤勯柤鍝ユ暩娴犳碍绻濋悽闈涗粶妞ゆ洦鍙冨畷妤€螣娓氼垰娈ㄥ銈嗗姂閸婃牜鈧碍姘ㄩ埀顒傛嚀婢瑰﹪宕伴弽褉鏋旈柕濠忓缁♀偓闂佹眹鍨藉ḿ褎鐗庣紓浣哄亾濠㈡ḿ绮旈悷閭﹀殨闁哄被鍎辩粻鐢告煙閻戞ḿ绠橀柛鐐垫暬閺岋綁鎮╅悜姗嗕哗闁诲繐绻堥崝宀勵敊韫囨稑唯鐟滃宕戦幘鑸靛枂闁告洦鍓欑喊宥呪攽閳藉棗浜濈紒璇插€块敐鐐剁疀濞戞瑦鍎梺闈╁瘜閸橀箖鏁嶅⿰鍐f斀闁宠棄妫楅悘鐘绘煙绾板崬浜伴柨婵堝仜椤撳ジ宕堕埡鍐跨闯濠电偠鎻紞渚€藟閹捐绀夌€广儱顦伴悡娆戠磼鐎n亞浠㈤柡鍡涗憾閺岋綁鏁愰崶褍骞嬪Δ鐘靛仜椤戝寮崘顔肩劦妞ゆ帒鍊绘稉宥呪攽閻樺磭顣查柛瀣剁秮閺屾盯濡烽幋婵嗘殶濡ょ姴娲幃妤冩喆閸曨剙纰嶇紓浣割槹閹告娊鍨鹃弮鍫濈妞ゆ柨妲堣閺屾盯鍩勯崗鐙€浜Λ鍕吋閸モ晝锛濇繛杈剧到婢瑰﹪宕曢幇鐗堢厱闁靛ǹ鍎遍。宕囩磼椤旂⒈鍎忔い鎾冲悑瀵板嫮鈧綆浜栭崑鎾绘煥鐎c劋绨婚梺鐟版惈缁夊爼藝閿旈敮鍋撳▓鍨灈闁诲繑绻堥崺鐐哄箣閿曗偓閻擄繝鏌涢埄鍐炬畼濞寸媭鍨跺娲川婵犲海鍔堕梺鍛婃处閸欏骸煤閸涘﹣绻嗛柕鍫濈箳閸掍即鏌涢悤浣哥仸鐎规洘鍔欏畷褰掝敃閿濆懎浼庢繝纰樻閸ㄦ娊宕㈣缁傚秵銈i崘鈺佲偓鍨叏濡厧浜鹃悗姘炬嫹

一、 配置eclipes开发环境
首先,下载需要的插件:
eclipse-SDK-
Eclipse IDE
官方下载地址:
http://download.eclipse.org/downloads/drops/R-
xmlbuddy_2.0.62
用于xml开发,可以用来编辑web.xml文件
官方下载地址:http://www.xmlbuddy.com/
org.objectweb.lomboz_
用于web开发,支持jsp,servlet等等的高亮显示和编辑
官方下载地址:http://forge.objectweb.org/PRojects/lomboz/
ajdt_1.2_for_eclipse_3.0
用于aspectJ开发,专为eclipse开发的AspectJ插件
官方下载地址:http://www.xmlbuddy.com/
VE-runtime-
安装以上两个插件时必备,一个相关的类包含在此插件中。The Eclipse Visual Editor project is a vendor-neutral, open development platform supplying frameworks for creating GUI builders, and exemplary, extensible tool implementations for Swing/JFC and SWT/RCP. These tools are exemplary in that they verify the utility of the Eclipse Visual Editor frameworks, illustrate the appropriate use of those frameworks, and support the development and maintenance of the Eclipse Visual Editor Platform itself.
官方下载地址:http://www.xmlbuddy.com/
GEF-runtime-
安装VE时必备,The Graphical Editing Framework (GEF) allows developers to take an existing application model and quickly create a rich graphical editor.
官方下载地址:
http://download.eclipse.org/tools/gef/downloads/drops/R-
emf-sdo-runtime-
安装VE时必备,EMF is a modeling framework and code generation facility for building tools and other applications based on a strUCtured data model. From a model specification described in XMI, EMF provides tools and runtime support to produce a set of java classes for the model, a set of adapter classes that enable viewing and command-based editing of the model, and a basic editor. Models can be specified using annotated Java, XML documents, or modeling tools like Rational Rose, then imported into EMF. Most important of all, EMF provides the foundation for interOperability with other EMF-based tools and applications.
官方下载地址:
http://download.eclipse.org/tools/emf/downloads/drops/
[说明] 现在最高版本的ajdt支持到eclipse
2、下载完成后解压进行安装:help -> softwareupdate -> find and install -> …from local….
3、进行配置window -> perspective
配置aspectj:
不需要非凡配置
配置lomboz:
A、设置jdk tools.jar位置,为安装的j2sdk目录
B、配置server definition:选择Apache Tomcat : tomcat5.0.x,设置其Server lib和project lib,可以把%tomcat_home%/common/lib一些相关的包都放进去,有可能是必须放,注重,某些版本据说可能是
\servers,tomcat5.0.x配置文件tomcat50x.server文件,将两处-Djava.endorsed.dirs=,
修改为如下:
-Djava.endorsed.dirs="${serverRootDirectory}/common/endorsed"
4、配置window -> customize perspective,将aspectj和lomboz相关的项目都给添加到new中,这样就可以通过“新建“来建立相应的工程文件。
二、 集成AspectJ到Web工程中
5、新建一个AspectJ工程
6、在此工程中新建一个Lomboz J2EE Module
7、新建一个Servlet,并设置其url-mapping
/*
* Created on
*
* @Author:Jonathan Q. Bo from tju.MSNrl
* MyBlog:http://blog.csdn.net/jonathan_q_bo
*
*/
package org.tju.msnrl.jonathan.aspectj;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
* 2005-8-3 14:46:22
*/
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
ServletOutputStream out = response.getOutputStream( );
out.println("<h1>Hello World from an aspect-oriented Servlet!</h1>");
}
}
8、新建一个Aspect,拦截其doGet(..),在其执行之前和之后作相应的advice
/*
* Created on
*
* @Author:Jonathan Q. Bo from tju.msnrl
* MyBlog:http://blog.csdn.net/jonathan_q_bo
*
*/
package org.tju.msnrl.jonathan.aspectj;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
* 2005-8-3 15:43:54
*/
public aspect HelloWorldAspect {
public pointcut captureHttpRequest(HttpServletRequest request,
HttpServletResponse response) :
execution(public void HelloWorld.doGet(HttpServletRequest,
HttpServletResponse)) &&
args(request, response);
before(HttpServletRequest request, HttpServletResponse response)
throws IOException :
captureHttpRequest(request, response)
{
response.setContentType("text/Html");
ServletOutputStream out = response.getOutputStream( );
out.println("<html>");
out.println("<head><title>Adding a title using AspectJ!</title></head>");
out.println("<body>");
}
after(HttpServletRequest request, HttpServletResponse response)
throws IOException :
captureHttpRequest(request, response)
{
ServletOutputStream out = response.getOutputStream( );
out.println("</body>");
out.println("</html>");
}
}
9、部署工程,默认会将工程打包成war文件部署,试验发现,war文件执行时会报错,所以,需要改写build.xml文件,原build.xml文件将编译好的文件统一放到dist文件夹中,然后对其打包war,简单修改,只需要将打包过程去掉,将dist文件夹直接拷贝到tomcat_home/webapps/下就可以了
<project name="webmodulebuilder" default="deploy" basedir=".">
<!-- set global properties for this build -->
<property file="build.properties"/>
<property name="dist" value="../../dist" />
<property name="deploy.dir" value="C:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/rbac" />
<property name="web" value="../" />
<target name="init">
<!-- Create the dist directory structure used by compile
and copy the deployment descriptors into it-->
<mkdir dir="${dist}"/>
- ››基于IP地址的vsftp服务器
- ››基于MySQL 水平分区的优化示例
- ››基于CentOS5的Linux下pptp和openvpn的搭建及配置
- ››基于JavaScript的网页版塔防游戏
- ››基于Android平台 QQ大战360手机游戏爆红
- ››基于Windows Azure的云计算应用设计
- ››基于AES算法实现对数据的加密
- ››基于SoPC目标板Flash编程设计的创建及应用
- ››基于SolidWarks齿轮机构的运动分析与仿真
- ››基于Windwos Server 2008故障转移群
- ››基于JavaScript的REST客户端框架
- ››基于JavaScript和CSS的Web图表框架横向对比
更多精彩
赞助商链接