AspectJ 注释在 JAVA maven 项目不是 spring 项目中不起作用 注解必须具有 RUNTIME 范围方面必须有 @Aspect 注释避免双日志输出添加缺少的 Maven 插件执行删除多余的 AspectJ 依赖确保旧的 AspectJ Maven 插件使用最新的 AspectJ 编译器

问题描述

我正在尝试在没有 spring 的 JAVA maven 项目中实现 AspectJ 注释。我添加了方面并创建了注释。但它没有调用我作为注释添加方法的方面..下面是我的代码..还有项目链接 - https://github.com/chandru-kumar/aop-example

我也添加了aspectj maven插件..但它没有被调用..你能帮忙..吗?不知道我错过了什么。 我没有找到任何没有 Spring 项目的例子..

Pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aop.example</groupId>
  <artifactId>aop-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <properties>
        <java.version>1.8</java.version>
    </properties>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
            
            <!-- <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <complianceLevel>1.8</complianceLevel>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </execution>
                </executions>
            </plugin> -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        com.aop.example.Test
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

JAVA - 方面 - 建议

package com.aop.advices;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;

public class SysoutAdvice {
    @Around("@annotation(com.annotations.Sysout)")
    public Object print(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("Start....");
        Object object = proceedingJoinPoint.proceed();
        System.out.println("End....");
        return object;
    }
}

JAVA - 注释

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@retention(RUNTIME)
@target(METHOD)
public @interface Sysout{}

解决方法

我可以看出您是 AspectJ 初学者。你犯了很多错误。不过别担心,随着时间的推移,您会学习并获得更多经验。

注解必须具有 RUNTIME 范围

否则编译后的代码不会有注释拦截,只有源代码,无济于事。

enter image description here

方面必须有 @Aspect 注释

否则 AspectJ 编译器不会将该类识别为方面。

enter image description here

避免双日志输出

如果不添加 && execution(* *(..)),切面将同时拦截 callexecution 连接点。效果将是双日志消息,因为方面建议被触发两次。

enter image description here

添加缺少的 Maven 插件执行

否则 AspectJ Maven 插件不会编译任何东西,因为您没有告诉它要做什么。

enter image description here

删除多余的 AspectJ 依赖

否则,Assembly 插件会将它们打包到可执行 JAR 中,将其压缩到 13.4 MB。但实际上,当使用编译时编织时,您只需要 AspectJ 运行时 aspectjrt 即可运行应用程序。其他两个用于加载时编织 (aspectjweaver) 和 AspectJ 编译器 (aspetjtools),您在运行时不需要这两者。

如果您按照我的建议删除这两个,JAR 大小会急剧缩小到 0.12 MB。这小了 100 多倍。

enter image description here

确保旧的 AspectJ Maven 插件使用最新的 AspectJ 编译器

版本号应与用于 aspectjrt 的版本号相同。所以你不需要 aspetjtools 作为运行时依赖,而是作为插件依赖,如果你想确保你有相同的版本。

enter image description here

这一步是可选的,但 AspectJ Maven 1.11 默认使用 AspectJ Tools 1.8.13。如果你只是编译 Java 8 代码,这很好,因为 AspectJ Maven 1.11 不支持超过 Java 8。

如果您想要一个支持 AspectJ 1.9.7.M3 和 Java 16 的更现代的插件,请查看 dev.aspectj plugin version(请注意其他 Maven 组 ID!)。

在您的 Maven 配置中应该更改其他次优的东西,但这是最重要的东西,它使您的项目运行并且您的可执行 JAR 变小。


更新:这是我的 pull request,它修复了上述问题以及其他一些问题(请参阅提交评论)。