English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot AOP @AfterReturning

After returningはSpring AOPのアドバイスであり、通常、コネクションポイントが実行(実行)された後に呼び出されます。例外が発生した場合、呼び出されません。以下を使用できます @AfterReturning アプリケーションで提案が返信された後に実施されます。アノテーションは、ポイントカットに覆われるメソッドの前に実行される提案として機能をタグ付けます。

提案が正常に実行され、返信値が得られた後、その提案が実行されます。return属性で定義された名前は、adviceメソッドのパラメータ名と一致する必要があります。メソッドが返信値を持って戻るとき、その値は通知メソッドの対応するパラメータ値として渡されます。

アプリケーションで通知が返信された後に実現します。

SpringBoot AOP @AfterReturning

ステップ1: Spring Initializrを開く http://start.spring.io を開きます。

ステップ2: 提供 Group 名前。私たちはグループ名を提供しています com.w3codebox。

ステップ3: 提供しています Artifact Id。Artifact Idを提供します aop-after-returning-advice-example。

ステップ4: 追加 Spring Web 依存関係。

ステップ5: クリック 生成ボタン。"生成"ボタンをクリックすると、すべての規範をパッケージします。 jar 文件中,并将其下载到本地系统。

ステップ6: 提取

第7步: 使用以下步骤导入文件夹:

文件->导入->现有Maven项目->下一步->浏览文件夹 aop-returning-advice-example示例->完成。

ステップ8: オープン pom.xml 文件并添加以下 AOP 依赖项。它是使用 Spring AOP そして AspectJ 面向方面编程的入门。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</<dependency>
</<dependencies>
pom.xml
<?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>com.w3codebox</groupId>
<artifactId>aop-after-returning-advice-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aop-after-returning-advice-example</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</<dependency>
</<dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

ステップ9: src/main/java フォルダを作成 に名前の com.w3codebox.model のパッケージを作成

ステップ10: 在包 com.w3codebox.modelに名前の Account 的类。

"Account"クラスにおいて、以下の操作を行います:

Stringの型の二つの変数を定義しました accountNumber そして accountType ファイルを右クリック->ソース-> フィールドを使用してコンストラクタを生成 Gettersを生成。
ファイルを右クリック-> Source-> Generate Getters and Setters-> Gettersを選択-> Generate
生成 toString()
ファイルを右クリック->ソース->toString()...を生成

Account.java

package com.w3codebox.model;
public class Account 
{
    private String accountNumber;
    private String accountType;
    public Account(String accountNumber, String accountType) 
    {
        super();
        this.accountNumber = accountNumber;
        this.accountType = accountType;
    }
    public String getAccountType() 
    {
        return accountType;
    }
    public String getAccountNumber() 
    {
        return accountNumber;
    }
    @Override
    public String toString()
    {
        return "Account [accountNumber=" + accountNumber+ ", accountType=" + accountType + "]";
    }
}

ステップ11: 別の名前の com.w3codebox.service.implのパッケージ。

ステップ12: このプログラムパッケージ内に、名前が AccountServiceImpleのクラス。

このクラスでは、アカウントサービスを定義しています。

AccountServiceImpl。 Java

package com.w3codebox.service.impl;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.stereotype.Service;
import com.w3codebox.model.Account;
@Service
public class AccountServiceImpl implements AccountService 
{
    //HashMapにアカウント詳細を保存します。
    private static Map<String,Account> map = null;
    static
    {
        map = new HashMap<>();
        //マップにアカウント詳細を追加します。
        map.put("M4546779", new Account("10441117000", "Saving Account"));
        map.put("K2434567", new Account("10863554577", "Current Account"));
    }
    @Override
    public Account getAccountByCustomerId(String customerId) throws Exception
    {
    if(customerId ==null)
    {
        throw new Exception("Invalid! Customer Id");
    }
    Account account= null;
    Set<Entry<String, Account>> entrySet = map.entrySet();
    for (Entry<String, Account> entry : entrySet) 
    {
        if(entry.getKey().equals(customerId))
        {
            account= entry.getValue();
        }
    }
    return account;
    }
}

ステップ13: com.w3codebox.service.impl。パッケージ内に、名前が AccountService のインターフェース。

AccountService.java

package com.w3codebox.service.impl;
import com.w3codebox.model.Account;
//クライアントIDが見つからない場合、例外を発生させるインターフェースを作成しています。
public interface AccountService 
{
    public abstract Account getAccountByCustomerId(String customerId) throws Exception;
}

ステップ14: 创建名称为 com.w3codebox.aspectのパッケージ。

ステップ15: 在包 com.w3codebox.aspect 中创建一个名称为 AccountAspect 的类。

在该类中,我们使用注解 @AfterReturning。 我们还定义了 afterReturningAdvice()方法。

注意: 我们在 中定义的name(account) > returning 属性必须与 advice方法中的参数名称相对应。

AccountAspect.java

package com.w3codebox.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import com.w3codebox.model.Account;
@Aspect
@Component
public class AccountAspect 
{
//implementing after returning advice   
@AfterReturning(value="execution("* com.w3codebox.service.impl.AccountServiceImpl.*(..))",returning="account")
public void afterReturningAdvice(JoinPoint joinPoint, Account account)
{
System.out.println("After Returing method:")+joinPoint.getSignature());
System.out.println(account);
}
}

ステップ16: オープン AopAfterReturningAdviceExampleApplication.java 文件并添加注解 @EnableAspectJAutoProxy。

注解支持处理带有AspectJ的 @Aspect アノテーションのコンポーネント。それと@Configurationアノテーションを一緒に使用します。

を用いる@EnableAspectJAutoProxyアノテーションを使用しています proxyTargetClass 属性です。属性 proxyTargetClass = true を使用することができます CGLIB (コード生成ライブラリ)プロキシを使用し、デフォルトのインターフェースベースのJDKプロキシメソッドではなく、代理を生成します。

ConfigurableApplicationContext はインターフェースであり、ApplicationContextのアプリケーションコンテキストクライアントメソッドに加えて、アプリケーションコンテキストの設定に使用するツールも提供します。

AopAfterReturningAdviceExampleApplication.java

package com.w3codebox;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.w3codebox.model.Account;
import com.w3codebox.service.impl.AccountService;
import com.w3codebox.service.impl.AccountServiceImpl;
@SpringBootApplication
//@EnableSpectProxy注解支持处理用@Aspect注解标记的组件。它类似于xml配置中的标记。
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AopAfterReturningAdviceExampleApplication
{
    public static void main(String[] args)  
    {
    ConfigurableApplicationContext ac = SpringApplication.run(AopAfterReturningAdviceExampleApplication.class, args);
    //アプリケーションコンテキストからaccountオブジェクトを取得します
    AccountService accountService = ac.getBean("accountServiceImpl", AccountServiceImpl.class);
    Account account;
    try 
    {
        account = accountService.getAccountByCustomerId("K2434567");
        if(account != null)
            System.out.println(account.getAccountNumber())+"\t"+account.getAccountType());
        } 
        catch (Exception e) 
        {
            System.out.println(e.getMessage());
        }
    }
}

すべてのクラスとパッケージを作成した後、プロジェクトのディレクトリ構造は以下のようになります:

ステップ17: オープン AopAfterReturningAdviceExampleApplication.java ファイルを開き、Javaアプリケーションとして実行します。以下のように出力が表示されます:

次の節で、提案を出した後で理解します。