如何解决在两次宽高比打印之前
我有以下@Before Aspect,其中如果用我的“自定义注释”注释方法, 我会进入进行打印的Aspect。
Aspect工作正常,但打印两次,这意味着我两次输入了Aspect。
请问我为什么只发生一次却为什么会发生两次呢?谢谢。
注意:这不是在Spring中,这是用纯Java实现的。
带有注释以触发Aspect的方法的类
public class ClassUsingUser {
@RequestValidation(isEnabled = true)
public String doSomething(User user) {
return user.toString();
}
}
自定义注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequestValidation {
boolean isEnabled() default false;
}
方面类
@Aspect
public class RequestValidationAspect {
@Before("@annotation(RequestValidation)")
public void before(JoinPoint joinPoint) {
// this is the issue where I get this printed twice.
// but expecting it to happen only once.
System.out.println("Entered Aspect!!");
}
}
public class SomeMainClass {
public static void main(String[] args) {
User user1 = new User();
user1.setName("Fer");
ClassUsingUser classUsingUser = new ClassUsingUser();
// based on debug,I seem to hit the following doSomething() line,go to Aspect @Before,// come back here and go into Aspect @Before again thus printing twice.
String s = classUsingUser.doSomething(user1);
System.out.println(s);
}
}
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</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>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。