公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了为什么AspectJ@Around建议要执行两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的AspectJ示例,我已经将其作为&Hello world"风格的概念证明。StyleAspect中的建议代码似乎执行了两次,即使SomeClass中的实际代码只执行一次(根据需要)。

代码如下:

首先,一个名为WithStyle的批注:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface WithStyle {  
}

然后,一个截取带有@WithStyle注释的任何代码的方面

@Aspect
public class StyleAspect {

    @Around("@annotation(WithStyle)")
    public Object doItWithStyle(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("Doing it in style...");
        Object result = pjp.proceed();
        System.out.println("Done");
        
        return result;
    }
}

最后是带有注释的一些代码

public class SomeClass {
    
    @WithStyle
    public void doIt() {
        System.out.println("I'm doing it....");
    }
}

当我运行此命令时,我得到以下输出:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ AspectJTest ---
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done

因此,似乎代码本身只执行一次,而方面中的代码执行两次。

调用代码如下:

public class Main {
    
    public static void main(String[] args) {
        SomeClass someClass = new SomeClass();
        someClass.doIt();
    }
}

为完整起见,我将POM包含在AspectJ插件配置中

<?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>ie.philb</groupId>
    <artifactId>AspectJTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <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>
        <project.build.java.target>1.8</project.build.java.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                            <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
      
</project>

推荐答案

您的around()建议正在截取用@WithStyle(即doIt())注释的方法的call和execution连接点。如果将System.out.println(pjp);添加到您的方面:

@Aspect
public class StyleAspect {

    @Around("@annotation(WithStyle) ") 
    public Object doItWithStyle(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println(pjp);
        System.out.println("Doing it in style...");
        Object result;
        try{
            result = pjp.proceed();
        }
        finally{
            System.out.println("Done");
        }
        return result;
    }
}

您将得到以下输出:

call(public void SomeClass.doIt()) <----
Doing it in style...
execution(public void SomeClass.doIt()) <----
Doing it in style...
I'm doing it....
Done
Done

您可以清楚地看到SomeClass.doIt()方法的连接点call和execution正在被around建议doItWithStyle截获。

从call的拦截中,around建议正在编织代码如下:

// around advice code  before the pjp.proceed();
someClass.doIt(); // during the pjp.proceed();
// around advice code  after the pjp.proceed();

因此:

 System.out.println("Doing it in style...");.
 someClass.doIt();
 System.out.println("Done");

从执行:

@WithStyle
public void doIt() {
    // around advice code  before the pjp.proceed();
    System.out.println("I'm doing it....");
  // around advice code  after the pjp.proceed();
}

因此:

@WithStyle
public void doIt() {
    System.out.println("Doing it in style...");
    System.out.println("I'm doing it....");
    System.out.println("Done");
}

导致输出:

Doing it in style... 
Doing it in style...
I'm doing it....
Done
Done

现在,如果要避免around建议同时拦截call和execution方法的execution。您需要进一步限制around建议截获的连接点。要只截取call方法,您可以执行以下操作:

 @Around("@annotation(WithStyle) && call(* *(..))") 

方法execution:

@Around("@annotation(WithStyle) && execution(* *(..))") 

您可以通过调整call或execution切入点的签名,根据方法的参数数量、其返回类型、名称等进一步限制被截取的连接点。

这篇关于为什么AspectJ@Around建议要执行两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:AspectJ 两次 建议 执行
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定