实施方面示例不起作用
我尝试从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对不起,我很忙。假设您的目录布局是这样的:
某些示例类可能看起来像这样:
您的pom是:
然后,如果您运行
,则MVN清洁测试
或简单地将项目导入Intellij Idea,日志输出将为:现在,如果出于任何原因您也想将方面应用于注释的应用程序类,只需
src/ test/java
src/main/java
,&lt; scope&gt; test&lt;/scope&gt;
fromappect> expack> expact j.rt
and code> and code然后,控制台日志是:
看到?应用程序类
myClass
在这种情况下也被截获。更新:关于为什么它在您自己的示例中不起作用,那是因为您的POM的配置方式使Maven Compiler插件在编织了我的Facteack j Maven Plugin之后重新编译了您的类。结果是由于覆盖的类文件而丢失了方面信息。
Sorry, I was busy. Assuming that your directory layout is like this:
Some sample classes might look like this:
Your POM would be:
Then if you either run
mvn clean test
or simply import the project into IntelliJ IDEA, the log output will be:Now if for whatever reason you also want to apply the aspect to the annotated application class, simply
src/test/java
tosrc/main/java
,<scope>test</scope>
fromaspectjrt
and<goal>compile</goal>
.Then the console log would be:
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.