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

SpringBoot AOP @Around

نصحية Around من @Around التعليق يُظهر. إنه يُنفيذ قبل وبعد نقطة الاتصال. هذا هو النصحية الأقوى. كما يوفر المزيد من التحكم للمستخدمين النهائيين، مما يتيح لهم التعامل مع ProceedingJoinPoint.

لنقم بتنفيذ النصحية حول التطبيق.

Spring Boot @Around مثال

步骤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("method call before aroundAdvice() method" + jp.getSignature().getName() + " method");
        try 
        }
            jp.proceed();
        } 
        finally 
        }
        }
        System.out.println("method call after aroundAdvice() method" + jp.getSignature().getName() + " method");
    }
}

الخطوة 13: فتح AopAroundAdviceExampleApplication.java ملف، وأضف العلامة @EnableAspectJAutoProxy。

العلامة التي تتيح الدعم لمعالجة العلامات المزورة بـ AspectJ @Aspect المكونات الملاحظة. يستخدم مع علامة @Configuration.

الملخص هو واجهة، بالإضافة إلى طرق العملاء لـ ApplicationContext في تطبيق، يقدم أدوات لضبط تطبيق 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.close();
    }
}

بعد إنشاء جميع الملفات والمستودعات، سيكون بناء المشروع كالتالي:

الآن، تشغيل التطبيق.

الخطوة 14: فتح AopAroundAdviceExampleApplication.java ومن ثم تشغيل تطبيق Java.

في الصادرات العلوية، رأينا أن طريقة aroundAdvice() ستنشغل مرتين. أولاً، في تنفيذ displayBalance()قبل الطريقة، أولاً، في تنفيذ displayBalance()بعد الطريقة. يُسمى الاستشارة.