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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章