在两次宽高比打印之前

问题描述

我有以下@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!!");
    }
}

仅触发方法和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);
    }
}

POM文件中的依赖项和插件

<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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...