日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了為什么AspectJ@Around建議要執(zhí)行兩次?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有下面的AspectJ示例,我已經(jīng)將其作為&Hello world"風(fēng)格的概念證明。StyleAspect中的建議代碼似乎執(zhí)行了兩次,即使SomeClass中的實(shí)際代碼只執(zhí)行一次(根據(jù)需要)。

代碼如下:

首先,一個(gè)名為WithStyle的批注:

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

然后,一個(gè)截取帶有@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....");
    }
}

當(dāng)我運(yùn)行此命令時(shí),我得到以下輸出:

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

因此,似乎代碼本身只執(zhí)行一次,而方面中的代碼執(zhí)行兩次。

調(diào)用代碼如下:

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

為完整起見(jiàn),我將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())注釋的方法的callexecution連接點(diǎn)。如果將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()方法的連接點(diǎn)callexecution正在被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");

從執(zhí)行:

@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");
}

導(dǎo)致輸出:

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

現(xiàn)在,如果要避免around建議同時(shí)攔截callexecution方法的execution。您需要進(jìn)一步限制around建議截獲的連接點(diǎn)。要只截取call方法,您可以執(zhí)行以下操作:

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

方法execution

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

您可以通過(guò)調(diào)整callexecution切入點(diǎn)的簽名,根據(jù)方法的參數(shù)數(shù)量、其返回類(lèi)型、名稱(chēng)等進(jìn)一步限制被截取的連接點(diǎn)。

這篇關(guān)于為什么AspectJ@Around建議要執(zhí)行兩次?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:AspectJ 兩次 建議 執(zhí)行
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定