簡體   English   中英

在沒有Spring的情況下使用AspectJ批注

[英]Using AspectJ annotations without Spring

我是AOP的新手。 我試圖在沒有Spring的情況下使用AspectJ在Maven項目中創建注釋。 但是我沒有嘗試使用@Aspect調用的方法。

這就是我的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>test</groupId>
<artifactId>tanvi</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.5.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.5.3</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

注釋如下所示:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HasAccess {
    Access[] accesses();
    String message() default "You are not allowed to perform this operation";
}

我為注釋創建了注釋處理器:

@Aspect
public class HasAccessAdvice {
    //    @Before("execution(* *.*(..)) && @annotation(testAnnotation) ")
    @Before("execution(* *.*(..)) && @annotation(hasAccess)")
    public void myBeforeLogger(JoinPoint joinPoint, HasAccess hasAccess) {

    System.out.println("Okay - we're in the before handler...");
    System.out.println("The test annotation value is: " + hasAccess.accesses().toString());

    Signature signature = joinPoint.getSignature();
    String methodName = signature.getName();
    String stuff = signature.toString();
    String arguments = Arrays.toString(joinPoint.getArgs());
    System.out.println("Write something in the log... We are just about to call method: "
                       + methodName + " with arguments " + arguments + "\nand the full toString: "
                       + stuff);

}

}

我在這個電話中稱呼它:

public class TestMe {

    @HasAccess(accesses = {Access.CREATE_PURCHASE})
    public void createPurchase(BigDecimal bigDecimal) {
        System.out.println("create Purchase called");
    }
}

我創建了一個aop.xml文件,並將其與pom.xml放在同一文件夾中。

<aspectj>
    <aspects>
        <aspect name="HasAccessAdvice"/>
    </aspects>
</aspectj>

當我調用方法createPurchase時,它將在不首先調用@Before方法的情況下運行。 請幫我解決我所缺少的。 我發現的大多數文檔/答案都與Spring保持一致。 任何不使用Spring的教程或什至是創建簡單注釋的另一種方法的任何指針,將不勝感激。

首先,由於您使用的是aop.xml ,因此我假設您要進行加載時編織。 請參閱加載時間編織文檔不同編織類型的文檔

其次,在aop.xml文件中,定義要使用的<aspect> ,但是還需要定義要編織的類文件/包:

<aspectj>
    <aspects>
        <aspect name="HasAccessAdvice"/>
    </aspects>
    <weaver options="-verbose">
        <!-- weave anything -->
        <include within="*" />
        <!-- weave specific packages only -->
        <include within="my.package..*" />
    </weaver>    
</aspectj>

使用"*"在任何類上運行方面,或者將my.package替換為TestMe的包。 請注意,雙點..包括子包。
另請注意, <aspect name="...">要求提供帶有包的標准Aspect-name。 HasAccessibleAdvice在默認程序包中創建了HasAccessibleAdvice 否則,請添加您的包裹。

第三,aop.xml必須在類路徑的META-INF/aop.xml可讀。
如果通過CLI(使用java -javaagent... )運行測試,請檢查類路徑設置( -cp )。
如果您正在編寫JUnit測試,則可以將META-INF/aop.xml放在src/test/resources並調整pom.xml<build><plugins> ,以包括如下所示的加載時間編織器:

<properties>
    <aspectj.version>1.8.9</aspectj.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <argLine>
                    -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
                </argLine>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM