idea 16 AspectJ maven單元DEMO 測試 配置


這個DEMO 只能用作測試  不能用作maven 的 insertAll 和打包

我這邊在嘗試的使用AspectJ 來進行AOP的權限管理 然後非常的 ......

我這邊第一步就把我難住了 那就是idea對 AspectJ 的配置  搞了2個多小時才弄出來

ok 進入正題

我們需要 idea 16

maven

aspectjrt 1.8.9
aspectjtools 1.8.9

新建一個 maven項目  我沒有用用wabapp這個maven模版





點擊next


點擊下一步



在這裏需要注意 要添加 archetypeCatalog=internal 否則maven建立速度非常的慢



剛開始進去的時候可能 會是這個樣子 那麼我們就需要手動的配置下 文件  把java改源代碼文件夾


改變後


可能有部分的同學需要修改下settings  有的不用




這邊我把 aspectJ 和他的 tools上傳到我的資源了 需要的可以去拿  要不然自己下載很慢  


然後是POM的配置文件

就是這個樣子

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.mritd</groupId>
    <artifactId>Test1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Maven 編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- AspectJ 編譯插件 -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                            <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


然後是demo

java 測試 HelloWorld.java

package me.mritd.demo;

/**
 * Created by m1867 on 2016/10/28.
 */
public class HelloWorld {
    public static void main(int i){
        System.out.println("in the main method  i = " + i);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        main(5);
    }
}


然後是 AspjectJ

HelloAsoect.aj


package me.mritd.demo;

/**
 * Created by m1867 on 2016/10/28.
 */
public aspect HelloAspect {
   /* pointcut HelloWorldPointCut() : call(* main(int));*/
     pointcut HelloWorldPointCut() : execution(* main(int));


/*    before() : HelloWorldPointCut(){
        System.out.println("Entering : " + thisJoinPoint.getSourceLocation());
    }*/
    after() returning (Object o): HelloWorldPointCut() {
        System.out.println("Returned normally with " + o);
    }
    after() throwing (Exception e): HelloWorldPointCut() {
        System.out.println("Threw an exception: " + e);
    }
    after(): HelloWorldPointCut(){
        System.out.println("Returned or threw an Exception");
    }
}

ok

執行後的結果是

in the main method  i = 5
Returned normally with null
Returned or threw an Exception


Process finished with exit code 0


最後分享一個大牛的 教程 雖然我自己還沒有學會用

http://blog.csdn.net/zl3450341/article/category/1169602



感謝網絡上的大牛和前輩 的教程和指導  雖然demo出來了 但我還是沒有實現我想要的業務  哭

發佈了48 篇原創文章 · 獲贊 39 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章