java 方面没有在编译时织入目标类,尽管配置看起来是正确的

2vuwiymt  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(92)

我已经成功地将Aspects与CTW结合使用了一段时间,尽管总是用于编织我所链接的代码,而不是我所编译的代码。
test project中,重用允许我使CTW为外部类工作的配置,我一直试图编织我编译的代码,但没有成功。

我会很感激任何人谁可以帮助我发现无论它是我在这里做错了。

我尝试了callexecution两种方法,但都无济于事。
预期的行为是拦截com.budwhite.studying.aspect.logging.service包中的所有方法调用,在它们执行之前打印一条日志消息,在它们执行之后打印另一条日志消息。
我尝试将aspectj-report目标添加到aspectj-maven-plugin执行中,生成的文档 * 确实说 * 我的LoggingAspect正在通知该包中的类-但实际上,当这些方法被调用时,方面逻辑不会执行-并且我在.class文件中看不到任何编织方面代码的痕迹。
所有配置都可以在我链接的GH repo中找到,但这里是来自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;
    }
}
qhhrdooz

qhhrdooz1#

过度配置是您的问题的原因。只需要删除<sources/>部分,这将限制ANOJ编译器只能编译方面,而不能编译应该织入的其余代码。如果您这样做,您的示例项目将立即工作。
无论如何,这里是一个更精简的POM版本,使用dev.aspectj插件而不是Mojohaus插件(即使将其升级到1.14.0也足以用于Java 8或11)。到目前为止,Mojohaus插件仍然不支持Java 17+,而AspectJ.dev插件支持。

<?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.9.20.1</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>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>dev.aspectj</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.13.1</version>
        <configuration>
          <complianceLevel>${maven.compiler.source}</complianceLevel>
          <showWeaveInfo>true</showWeaveInfo>
          <Xlint>ignore</Xlint>
          <encoding>UTF-8</encoding>
        </configuration>
        <executions>
          <execution>
            <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>
    </plugins>
  </build>

</project>

请去掉你的 * aop.xml *,因为你没有使用加载时织入,也不应该在你的测试中使用,因为CTW根本不需要。
P.S.:这里有一个pull request给你。对不起,重新格式化文件缩进2而不是4个空格。我的IDE就是这样。当reviewing the diff

相关问题