aspectj compile time weaving - ajc$perSingletonInstance is null

62 Views Asked by At

I aimed to implement compile-time weaving in my codebase and discovered the AspectJ Maven plugin (https://dev-aspectj.github.io/aspectj-maven-plugin/).

aspectj.version = 1.9.21
maven-compiler.version = 3.11.0
java.version = 21

I made the following modifications:

Added the following code under the dependencies section:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version>
</dependency>

Included the following code under the <plugins> section:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler.version}</version>
    <executions>
        <execution>
            <id>default-compile</id>
            <configuration>
                <compilerArguments>
                    <d>${project.build.directory}/unwoven-classes</d>
                </compilerArguments>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>dev.aspectj</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.13.1</version>
    <configuration>
        <complianceLevel>${java.version}</complianceLevel>
        <showWeaveInfo>true</showWeaveInfo>
        <forceAjcCompile>true</forceAjcCompile>
        <sources/>
        <weaveDirectories>
            <weaveDirectory>${project.build.directory}/unwoven-classes</weaveDirectory>
        </weaveDirectories>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
</plugin>

Due to compatibility issues between Lombok and AspectJ, I initially compile to unwoven-classes before proceeding to target/classes. Running mvn clean compile results in a successful build with [INFO] Join point.... However, during application startup, I encounter the following error:

Caused by: org.aspectj.lang.NoAspectBoundException: Exception while initializing com.xyz.apirest.authorization.EnsureFromMonolith: java.lang.NoSuchMethodError: com.xyz.apirest.authorization.EnsureFromMonolith: method 'void <init>()' not found     at com.xyz.apirest.authorization.EnsureFromMonolith.aspectOf(EnsureFromMonolith.java:1)     at com.xyz.apirest.v1.internal.workflows.InternalDisbursementController.<clinit>(InternalDisbursementController.java:1)     ... 124 common frames omitted Caused by: java.lang.NoSuchMethodError: com.xyz.apirest.authorization.EnsureFromMonolith: method 'void <init>()' not found     at com.xyz.apirest.authorization.EnsureFromMonolith.ajc$postClinit(EnsureFromMonolith.java:1)     at com.xyz.apirest.authorization.EnsureFromMonolith.<clinit>(EnsureFromMonolith.java:1)     ... 125 common frames omitted

The relevant code snippet is as follows:

@Aspect
@Component
@RequiredArgsConstructor
public class EnsureFromMonolith {
    private final AuthenticationProfile authenticationProfile;
    private final XYZJwtAuthenticationConfig xyzJwtAuthenticationConfig;
    protected final Logger logger = LoggerFactory.getLogger(EnsureFromMonolith.class);

    @Around(value = "(execution(public * *(..)) && within(@com.xyz.apirest.authorization.RequireFromMonolith *)) || @annotation(com.xyz.apirest.authorization.RequireFromMonolith)")
    public Object ensureFromMonolith(ProceedingJoinPoint joinPoint) throws Throwable {
        if (authenticationProfile.getAid() == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        } else if (!validateProfile(authenticationProfile, xyzJwtAuthenticationConfig.getMonolithAuthorizedClientId())) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }

        return joinPoint.proceed();
    }

    public static boolean validateProfile(AuthenticationProfile authenticationProfile, String monolithClientId) {
        return authenticationProfile.getAid().equals(monolithClientId);
    }
}

I've tried the solutions recommended in AspectJ not working with compile time weaving and How to build aspectj project using maven? without success.

My goal is to have the application startup successfully with AspectJ functioning as intended.`

0

There are 0 best solutions below