尽管配置看似正确,但方面并未在编译时融入目标类中

问题描述 投票:0回答:1

我已经成功地将 Aspects 与 CTW 结合使用了一段时间,尽管总是用于编织我链接的代码,而不是我编译的代码。

在一个测试项目中,重用允许我使 CTW 适用于外部类的配置,我一直在尝试编写我编译的代码,但没有成功。

我将非常感谢任何可以帮助我发现我在这里做错的地方的人。

我尝试了callexecution方法,但没有成功。

预期的行为是拦截

com.budwhite.studying.aspect.logging.service
包中的所有方法调用,在执行前打印一条日志消息,并在执行后打印另一条日志消息。

我尝试将

aspectj-report
目标添加到
aspectj-maven-plugin
执行中,生成的文档 do say 我的
LoggingAspect
正在建议该包中的类 - 但实际上,当这些方法时,方面逻辑不会执行被调用 - 我在
.class
文件中看不到编织方面代码的任何痕迹。

所有配置都可以在我链接的 GH 存储库中找到,但以下是

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;
    }
}

java maven aspectj
1个回答
0
投票

过度配置是导致您的问题的原因。只需删除

<sources/>
部分,该部分限制 AspectJ 编译器仅编译切面,而不编译剩余的代码。如果您这样做,您的示例项目将立即生效。

无论如何,这是 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 根本没有必要。

© www.soinside.com 2019 - 2024. All rights reserved.