Java aop面向切面編程(aspectJweaver)案例詳解
面向切面編程的目的就是:在不改變別人的代碼的前提下,在別人代碼方法執(zhí)行前或后,執(zhí)行(切入自己的邏輯) 準(zhǔn)備:idea+maven+aspectjweaver-1.8.9.jar 結(jié)構(gòu)圖:
pom.xml內(nèi)容
<dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.9</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>1.8.9</version></dependency><dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version></dependency> </dependencies> <build><plugins> <plugin><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><executions> <execution><goals> <goal>compile</goal></goals> </execution></executions><configuration> <complianceLevel>1.8</complianceLevel> <source>1.8</source> <target>1.8</target> <aspectDirectory>src/main/java</aspectDirectory></configuration> </plugin></plugins> </build>
切面類
public aspect Staspect { public pointcut kkMethod(): execution(public String aop.Test.kk()); before(): kkMethod() {System.out.println('先執(zhí)行我'); }}
主類
public class Test { public String kk(){return '23'; } public static void main(String[] args) {Test test=new Test();System.out.println(test.kk()); }}
新建一個aop.xml(在META-INFO文件夾下)
<?xml version='1.0' encoding='UTF-8' ?><aspectj> <aspects><aspect name='aop.Staspect'/> </aspects> <weaver options='-XaddSerialVersionUID'></weaver></aspectj>
打jar包,執(zhí)行命令:mvn clean package 注意我的結(jié)構(gòu)圖,classes下面如果有兩個類就是編譯成功了 挑出jar包,執(zhí)行命令: java -javaagent:/home/admin/aspectjweaver-1.8.9.jar -classpath aspecttest-1.0-SNAPSHOT.jar aop.Test
注意:-javaagent后面的參數(shù)是你電腦aspectjweaver-1.8.9.jar的路徑
到此這篇關(guān)于Java aop面向切面編程(aspectJweaver)案例詳解的文章就介紹到這了,更多相關(guān)Java aop之a(chǎn)spectJweaver內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實現(xiàn)代碼2. Ajax實現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)3. jsp EL表達(dá)式詳解4. jsp中sitemesh修改tagRule技術(shù)分享5. JavaWeb Servlet中url-pattern的使用6. 爬取今日頭條Ajax請求7. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁中的JS 文件8. ASP基礎(chǔ)知識VBScript基本元素講解9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. JSP servlet實現(xiàn)文件上傳下載和刪除
