English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Around Advice wird von @Around Annotation bedeutet. Es führt vor und nach dem Verbindungspunkt aus. Dies ist die mächtigste Empfehlung. Es bietet dem Endbenutzer auch mehr Kontrolle, damit er ProceedingJoinPoint。
Lassen Sie uns um die Empfehlungen in der Anwendung herum implementieren.
Steps1: Öffnen Sie Spring Initializr http://start.spring.io .
Steps2: Bieten GruppeName. Wir bieten den Gruppennamen com.w3codebox.
Steps3: Bietet Artifact Id.Bieten Sie Artifact Id aop-around-advice-example。
Steps4: Hinzufügen Spring Web Abhängigkeiten.
Steps5: Klicken GenerierenSchaltfläche. Wenn wir auf die Schaltfläche "Generieren" klicken, packt sie alle Regeln in jar Datei und laden Sie sie auf das lokale System herunter.
Der6Schritt: EntpackenHeruntergeladene jar-Datei.
Steps7: Verwenden Sie die folgenden Schritte, um zu importierenOrdner:
Datei->Importieren->Bestehendes Maven-Projekt->Nächste Schritte->Durchsuchen Sie den Ordner aop-around-advice-example ->Fertig.
Steps8: Open pom.xml Datei und fügen Sie die folgenden AOP Abhängigkeiten. Es wird verwendet Spring AOP 和 AspectJ Einführung in die aspektorientierte Programmierung.
<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>
Steps9: 创建名称为 com.w3codebox.service的包。
Steps10: 在上面的包中创建一个名为 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."); } } }
Steps11: 创建另一个名为 com.w3codebox.aspect的包。
Steps12: 在上面的包中创建一个名为 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() { } //erklärt um die umgebende Benachrichtigung vor und nach der Übereinstimmung der Methode mit dem Einstichausdruck anzuwenden @Around(value= "logDisplayingBalance()") public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable { System.out.println("Der aroundAdvice() Methodenaufruf vor der Methodeaufruf ") + jp.getSignature().getName() + " method "); try { jp.proceed(); } finally { } System.out.println("Der aroundAdvice() Methodenaufruf nach der Methodeaufruf ") + jp.getSignature().getName() + " method "); } }
Steps13: Open AopAroundAdviceExampleApplication.java Datei und fügen Sie die Annotation hinzu @EnableAspectJAutoProxy。
Diese Annotation aktiviert die Unterstützung für die Verarbeitung von AspectJ gekennzeichneten @Aspect komponenten mit Annotationen. Es wird zusammen mit der @Configuration-Annotation verwendet.
ConfigurableApplicationContext ist eine Schnittstelle, die neben den ApplicationContext-Anwendungscontext-Clientmethoden auch Werkzeuge zur Konfiguration des Anwendungscontexts bereitstellt.
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 的组件。它类似于 xml 配置中的标记。 @EnableAspectJAutoProxy public class AopAroundAdviceExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args); //Get the employee object from the application context. BankService bank = context.getBean(BankService.class); //Display account balance String accnumber = "12345"; bank.displayBalance(accnumber); //Close the context object context.close(); } }
After creating all packages and classes, the project directory is as follows:
Now, run the application.
Steps14: Open AopAroundAdviceExampleApplication.java and run the Java application as a runtime application.
In the above output, we see that the method aroundAdvice() is called twice. First, during the execution displayBalance()Before the method, first, during the execution displayBalance()After the method. It is called counseling.