English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Aroundアドバイスは @Around アノテーションを表す。それは接続ポイントの前後で実行されます。これは最も強力なアドバイスです。また、最終ユーザーに多くの制御権を提供し、彼らが処理 ProceedingJoinPoint。
アプリケーション内のアドバイスを実施するために周りにいます。
ステップ1: Spring Initializrを開きます http://start.spring.io 。
ステップ2: 提供 グループ名前。私たちはグループ名を提供しています com.w3codebox。
ステップ3: 提供しています Artifact Id。提供Artifact Id aop-around-advice-example。
ステップ4: 追加 Spring Web 依存関係。
ステップ5: クリック 生成ボタン。"生成"ボタンをクリックすると、すべての規定をパッケージ化して jar ファイルからダウンロードして、ローカルシステムに保存します。
第6手順: 展開ダウンロードしたjarファイル。
ステップ7:以下の手順でインポートフォルダー:
ファイル->インポート->既存のMavenプロジェクト->次へ->フォルダーをブラウズ aop-around-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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.w3codebox</groupId> <artifactId>aop-around-advice-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>aop-around-advice-example</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
ステップ9: 名前が com.w3codebox.serviceのパッケージ。
ステップ10: 上記のパッケージに BankService のクラス。
このクラスでは、 displayBalance()メソッド。 それがアカウント番号をチェックし、一致すれば総額を返し、不一致であればメッセージを返します。
BankService.java
package com.w3codebox.service; import org.springframework.stereotype.Service; @Service public class BankService { public void displayBalance(String accNum) { System.out.println("Inside displayBalance() method"); if(accNum.equals("12345")) { System.out.println("Total balance:") 10,000"); } else { System.out.println("Sorry! wrong account number."); } } }
ステップ11: 別の com.w3codebox.aspectという名前のパッケージを作成します。
ステップ12: 上記のパッケージに BankAspectという名前のクラスを定義しています。
以下のクラスでは、 logDisplayingBalance()および aroundAdvice()メソッドのメソッド。
BankAspect.java
package com.w3codebox.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; //アプリケーションでSpring AOP機能を有効にする @Aspect @Component public class BankAspect { //すべての利用可能なメソッドを表示し、すべてのメソッドの呼び出しに通知を設定する @Pointcut(value= "execution(* com.w3codebox.service.BankService.*(..))") private void logDisplayingBalance() { } //メソッドとシグネチャエクスプレッションが一致する前にと後に適用される around 通知を宣言 @Around(value= "logDisplayingBalance()") public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable { System.out.println("方法呼び出し前のaroundAdvice()メソッド" + jp.getSignature().getName() + " method "); try { jp.proceed(); } finally { } System.out.println("方法呼び出し後のaroundAdvice()メソッド" + jp.getSignature().getName() + " method "); } }
ステップ13: オープン AopAroundAdviceExampleApplication.java ファイルにアノテーションを追加 @EnableAspectJAutoProxy。
このアノテーションは、AspectJに標記された処理をサポートする機能を有効にします。 @Aspect 注釈のコンポーネント。それと @Configuration 注釈を一緒に使用します。
ConfigurableApplicationContext はインターフェースであり、ApplicationContextのアプリケーションコンテキストクライアントメソッドに加えて、アプリケーションコンテキストの設定に使用するツールも提供しています。
AopAroundAdviceExampleApplication.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.service.BankService; @SpringBootApplication //@EnableAspectJAutoProxy 注解支持处理标记为 @Aspect annotation 的组件。它类似于 xml 配置中的标记。 @EnableAspectJAutoProxy public class AopAroundAdviceExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args); //アプリケーションコンテキストからemployeeオブジェクトを取得します。 BankService bank = context.getBean(BankService.class); //口座残高を表示します String accnumber = "12345"; bank.displayBalance(accnumber); //contextオブジェクトを閉じます。 context.close(); } }
すべてのパッケージとクラスが作成された後、プロジェクトディレクトリは以下のようになります:
今、アプリケーションを実行します。
ステップ14: オープン AopAroundAdviceExampleApplication.java それをJava実行アプリケーションとして実行します。
上の出力では、aroundAdvice()メソッドが2回呼び出されることが見られます。まず、実行 displayBalance()メソッドの前に、次に、実行 displayBalance()メソッドの後。これはアドバイザリーと呼ばれます。