You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置看似正确,但Aspect未编译期织入目标类的排查求助

问题排查:AspectJ编译期织入(CTW)对自有代码失效

我此前一直成功使用AspectJ的*CTW(编译期织入)*技术,但均是针对依赖的外部代码进行织入。近期在一个测试项目中,复用此前对外部代码有效的配置,尝试对自行编译的代码进行织入却失败了。我尝试了call和execution两种切点表达式,但均无效果。

预期行为是拦截com.budwhite.studying.aspect.logging.service包内的所有方法调用,在执行前后分别打印日志。我给aspectj-maven-plugin添加了aspectj-report目标,生成的文档显示LoggingAspect已对该包内类提供增强,但实际调用方法时切面逻辑未执行,且.class文件中也无织入痕迹。

以下是execution方式的核心配置(该方式在其他项目中对外部代码织入有效):

pom.xml

<?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>com.budwhite.studying</groupId>
    <artifactId>aspect-logging</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <logback.version>1.3.11</logback.version>
        <aspectj.version>1.8.13</aspectj.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${maven.compiler.source}</complianceLevel>
                    <encoding>UTF-8</encoding>
                    <verbose>true</verbose>
                    <sources>
                        <source>
                            <basedir>src/main/java</basedir>
                            <includes>
                                <include>**/LoggingAspect.java</include>
                            </includes>
                        </source>
                    </sources>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>aspectj-report</goal>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <argLine>-XX:-UseSplitVerifier</argLine>
                    <argLine>
                        -javaagent:${user.home}/.m2/repository/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
                    </argLine>
                    <useSystemClassLoader>true</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

aop.xml

<aspectj>
    <weaver options="-verbose -showWeaveInfo">
        <include within="**.LoggingAspect"/>
        <include within="com.budwhite.studying.aspect.logging.service..*"/>
    </weaver>
    <aspects>
        <!-- Aspects -->
        <aspect name="**.LoggingAspect"/>
    </aspects>
</aspectj>

LoggingAspect.java

package com.budwhite.studying.aspect.logging.aspect;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.stream.Collectors;

import static java.lang.System.currentTimeMillis;

@Aspect
public class LoggingAspect {

    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
    @Around("execution(* com.budwhite.studying.aspect.logging.service..*(..))")
    public Object whatever(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println("EVEN IF I DID SOMETHING WRONG WITH SLF4J, THIS PRINTOUT SHOULD SHOW UP");
        logger.warn("{} begins, args are [{}]",
                thisJoinPoint.getSignature().getName(),
                Arrays.stream(thisJoinPoint.getArgs())
                        .map(Object::toString)
                        .collect(Collectors.joining(",")));
        long startTime = currentTimeMillis();
        Object result = thisJoinPoint.proceed();
        logger.warn("{} ends, execution time {} ms, output is {}",
                thisJoinPoint.getSignature().getName(),
                currentTimeMillis()-startTime,
                result!=null ? result : ""
                );
        return result;
    }
}

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:35:58