实施方面示例不起作用

发布于 2025-01-31 05:03:50 字数 6001 浏览 7 评论 0原文

我尝试从 https://www.baeldung.com/aspectj 。不同之处在于,我想在Junit TestClass上使用注释。我搜索了一段时间的解决方案,但找不到正确的提示。

我的pom.xml的一部分是

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.7</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
    </dependency>
</dependencies>

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
        <configuration>
            <complianceLevel>1.8</complianceLevel>
            <!--<source>1.8</source>
            <target>1.8</target>-->
            <showWeaveInfo>true</showWeaveInfo>
            <verbose>true</verbose>
            <Xlint>ignore</Xlint>
            <!--<encoding>UTF-8 </encoding>-->
        </configuration>
        <executions>
            <execution>
                <phase>process-sources</phase>
                <goals>
                    <!-- use this goal to weave all your main classes -->
                    <goal>compile</goal>
                    <!-- use this goal to weave all your test classes -->
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>       
</plugins>

我的注释:

package com.mb.mtpp.main.extension.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
public @interface Secured {
public boolean isLocked() default false;
}

我的方面:

package com.mb.mtpp.main.extension.aspectj;

import com.mb.mtpp.main.extension.annotations.Secured;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public final class SecuredMethodAspect {

@Pointcut("@annotation(secured)")
    public void callAt(Secured secured) {
}

@Before("callAt(secured)")
public void around(JoinPoint pjp, Secured secured) throws Throwable {
    System.out.println("++++++++test++++++++");
    //return secured.isLocked() ? null : pjp.proceed();
}
}
@Secured(isLocked = true)
public class DummyClassTest {

@Test
@Secured(isLocked = true)
public void dummyTest4(){
    log.info("Test 4");
}
}

建议是对类注释的提及,我在日志中看到了:

CLASSPATH component C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\plugins\maven\lib\maven3\boot\plexus-classworlds.license: java.util.zip.ZipException: error in opening zip file
[INFO] Join point 'staticinitialization(void com.mb.mtpp.main.dummy.DummyClassTest.<clinit>())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:23) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.dummy.DummyClassTest.dummyTest3())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:64) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.dummy.DummyClassTest.dummyTest4())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:71) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-call(void com.mb.mtpp.main.extension.DummyClass.function1())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:79) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-call(void com.mb.mtpp.main.extension.DummyClass.function2())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:80) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.extension.DummyClass.function1())' in Type 'com.mb.mtpp.main.extension.DummyClass' (DummyClass.java:8) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.extension.DummyClass.function2())' in Type 'com.mb.mtpp.main.extension.DummyClass' (DummyClass.java:13) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)

但是“ ++++++++++ test ++++++++++++++”没有显示在控制台中。它执行,因为建议不存在。我按照示例中注释了类,测试柜和方法。我尝试了不同的点数设置,但显示的是最好的。然后也建议班级。我不知道怎么了。

I'm try to implement a simple AspectJ annotation example from https://www.baeldung.com/aspectj. The difference is, that I want to use the annotation at a JUnit TestClass. I googled a while for the solution but don't find the right hint.

Part of my pom.xml

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.7</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
    </dependency>
</dependencies>

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
        <configuration>
            <complianceLevel>1.8</complianceLevel>
            <!--<source>1.8</source>
            <target>1.8</target>-->
            <showWeaveInfo>true</showWeaveInfo>
            <verbose>true</verbose>
            <Xlint>ignore</Xlint>
            <!--<encoding>UTF-8 </encoding>-->
        </configuration>
        <executions>
            <execution>
                <phase>process-sources</phase>
                <goals>
                    <!-- use this goal to weave all your main classes -->
                    <goal>compile</goal>
                    <!-- use this goal to weave all your test classes -->
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>       
</plugins>

My Annotation:

package com.mb.mtpp.main.extension.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
public @interface Secured {
public boolean isLocked() default false;
}

My Aspect:

package com.mb.mtpp.main.extension.aspectj;

import com.mb.mtpp.main.extension.annotations.Secured;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public final class SecuredMethodAspect {

@Pointcut("@annotation(secured)")
    public void callAt(Secured secured) {
}

@Before("callAt(secured)")
public void around(JoinPoint pjp, Secured secured) throws Throwable {
    System.out.println("++++++++test++++++++");
    //return secured.isLocked() ? null : pjp.proceed();
}
}
@Secured(isLocked = true)
public class DummyClassTest {

@Test
@Secured(isLocked = true)
public void dummyTest4(){
    log.info("Test 4");
}
}

The Advice is mentions to the class annotation, I see it in the logs:

CLASSPATH component C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\plugins\maven\lib\maven3\boot\plexus-classworlds.license: java.util.zip.ZipException: error in opening zip file
[INFO] Join point 'staticinitialization(void com.mb.mtpp.main.dummy.DummyClassTest.<clinit>())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:23) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.dummy.DummyClassTest.dummyTest3())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:64) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.dummy.DummyClassTest.dummyTest4())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:71) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-call(void com.mb.mtpp.main.extension.DummyClass.function1())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:79) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-call(void com.mb.mtpp.main.extension.DummyClass.function2())' in Type 'com.mb.mtpp.main.dummy.DummyClassTest' (DummyClassTest.java:80) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.extension.DummyClass.function1())' in Type 'com.mb.mtpp.main.extension.DummyClass' (DummyClass.java:8) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)
[INFO] Join point 'method-execution(void com.mb.mtpp.main.extension.DummyClass.function2())' in Type 'com.mb.mtpp.main.extension.DummyClass' (DummyClass.java:13) advised by before advice from 'com.mb.mtpp.main.extension.aspectj.SecuredMethodAspect' (SecuredMethodAspect.java:62)

But the log "++++++++test++++++++" is not shown in the console. It executes as the advice is not there. I annotated the class, the testcase and a method as in the example. I tried different settings for my Pointcut but the shown is the best. Then the class is also advised. I don't know what's wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好久不见√ 2025-02-07 05:03:50

对不起,我很忙。假设您的目录布局是这样的:

“

某些示例类可能看起来像这样:

package com.mb.mtpp.main.extension.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
public @interface Secured {
  boolean isLocked() default false;
}
package com.mb.mtpp.main.extension;

import com.mb.mtpp.main.extension.annotations.Secured;

public class MyClass {
  @Secured
  public void doSomething() {}
}
package com.mb.mtpp.main.extension.aspectj;

import com.mb.mtpp.main.extension.annotations.Secured;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public final class SecuredMethodAspect {
  @Pointcut("@annotation(secured) && execution(* *(..))")
  public void securedMethods(Secured secured) {}

  @Before("securedMethods(secured)")
  public void interceptSecuredMethods(JoinPoint joinPoint, Secured secured) {
    System.out.println(joinPoint);
  }
}
package com.mb.mtpp.main.extension;

import com.mb.mtpp.main.extension.annotations.Secured;
import org.junit.jupiter.api.Test;

@Secured(isLocked = true)
public class MyTest {
  @Test
  @Secured(isLocked = true)
  public void dummyTest4(){
    System.out.println("Test 4");
  }

  @Test
  @Secured(isLocked = true)
  public void dummyTest5(){
    System.out.println("Test 5");
    new MyClass().doSomething();
  }
}

您的pom是:

<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>org.example</groupId>
  <artifactId>SO_AJ_AspectNotTriggered_72344739</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M6</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
        <configuration>
          <complianceLevel>1.8</complianceLevel>
          <showWeaveInfo>true</showWeaveInfo>
          <verbose>true</verbose>
          <Xlint>ignore</Xlint>
          <encoding>UTF-8</encoding>
        </configuration>
        <executions>
          <execution>
            <goals>
              <!--<goal>compile</goal>-->
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

然后,如果您运行,则MVN清洁测试或简单地将项目导入Intellij Idea,日志输出将为:

execution(void com.mb.mtpp.main.extension.MyTest.dummyTest4())
Test 4
execution(void com.mb.mtpp.main.extension.MyTest.dummyTest5())
Test 5

现在,如果出于任何原因您也想将方面应用于注释的应用程序类,只需

  • 将方面从src/ test/java src/main/java
  • 删除&lt; scope&gt; test&lt;/scope&gt; from appect> expack> expact j.rt and code> and code
  • >代码>&lt;目标&gt;/goal&gt; 。

然后,控制台日志是:

execution(void com.mb.mtpp.main.extension.MyTest.dummyTest4())
Test 4
execution(void com.mb.mtpp.main.extension.MyTest.dummyTest5())
Test 5
execution(void com.mb.mtpp.main.extension.MyClass.doSomething())

看到?应用程序类myClass在这种情况下也被截获。


更新:关于为什么它在您自己的示例中不起作用,那是因为您的POM的配置方式使Maven Compiler插件在编织了我的Facteack j Maven Plugin之后重新编译了您的类。结果是由于覆盖的类文件而丢失了方面信息。

Sorry, I was busy. Assuming that your directory layout is like this:

Maven directory layout

Some sample classes might look like this:

package com.mb.mtpp.main.extension.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
public @interface Secured {
  boolean isLocked() default false;
}
package com.mb.mtpp.main.extension;

import com.mb.mtpp.main.extension.annotations.Secured;

public class MyClass {
  @Secured
  public void doSomething() {}
}
package com.mb.mtpp.main.extension.aspectj;

import com.mb.mtpp.main.extension.annotations.Secured;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public final class SecuredMethodAspect {
  @Pointcut("@annotation(secured) && execution(* *(..))")
  public void securedMethods(Secured secured) {}

  @Before("securedMethods(secured)")
  public void interceptSecuredMethods(JoinPoint joinPoint, Secured secured) {
    System.out.println(joinPoint);
  }
}
package com.mb.mtpp.main.extension;

import com.mb.mtpp.main.extension.annotations.Secured;
import org.junit.jupiter.api.Test;

@Secured(isLocked = true)
public class MyTest {
  @Test
  @Secured(isLocked = true)
  public void dummyTest4(){
    System.out.println("Test 4");
  }

  @Test
  @Secured(isLocked = true)
  public void dummyTest5(){
    System.out.println("Test 5");
    new MyClass().doSomething();
  }
}

Your POM would be:

<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>org.example</groupId>
  <artifactId>SO_AJ_AspectNotTriggered_72344739</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.9.7</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M6</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.14.0</version>
        <configuration>
          <complianceLevel>1.8</complianceLevel>
          <showWeaveInfo>true</showWeaveInfo>
          <verbose>true</verbose>
          <Xlint>ignore</Xlint>
          <encoding>UTF-8</encoding>
        </configuration>
        <executions>
          <execution>
            <goals>
              <!--<goal>compile</goal>-->
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

Then if you either run mvn clean test or simply import the project into IntelliJ IDEA, the log output will be:

execution(void com.mb.mtpp.main.extension.MyTest.dummyTest4())
Test 4
execution(void com.mb.mtpp.main.extension.MyTest.dummyTest5())
Test 5

Now if for whatever reason you also want to apply the aspect to the annotated application class, simply

  • move the aspect from src/test/java to src/main/java,
  • remove <scope>test</scope> from aspectjrt and
  • uncomment <goal>compile</goal>.

Then the console log would be:

execution(void com.mb.mtpp.main.extension.MyTest.dummyTest4())
Test 4
execution(void com.mb.mtpp.main.extension.MyTest.dummyTest5())
Test 5
execution(void com.mb.mtpp.main.extension.MyClass.doSomething())

See? Application class MyClass also gets intercepted in this case.


Update: Concerning why it was not working in your own example, that was because your POM was configured in such a way that Maven Compiler Plugin recompiled your classes after the aspects had been woven my AspectJ Maven Plugin. The result was that the aspect information was lost due to overwritten class files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文