English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Conseil Around par @Around Annotation représentative. Il s'exécute avant et après le point de connexion. C'est la plus puissante suggestion. Elle offre également plus de contrôle aux utilisateurs finaux, leur permettant de traiter ProceedingJoinPoint.
Laissez-nous mettre en œuvre les suggestions autour de l'application.
Étapes1: Ouvrir Spring Initializr http://start.spring.io .
Étapes2: Fourni GroupeNom. Nous avons fourni le nom de groupe com.w3codebox.
Étapes3: Fourni Artifact Id.Fournir Artifact Id aop-around-advice-example.
Étapes4: Ajouter Spring Web Dépendances.
Étapes5: Cliquez GénérerBouton. Lorsque nous cliquons sur le bouton "Générer", il enveloppe toutes les spécifications dans jar Fichier et le téléchargez sur le système local.
Le6Étape: ExtrayezTélécharger le fichier jar.
Étapes7: Utilisez les étapes suivantes pour importerDossier:
Fichier->Importer->Projet Maven existant->Suivant->Parcourir le dossier aop-around-advice-example ->Terminé.
Étapes8: Ouvrir pom.xml Fichier et ajoutez ce qui suit AOP Dépendances. C'est ce qui est utilisé Spring AOP et AspectJ Pour commencer à la programmation orientée aspect.
<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>
Étapes9: 创建名称为 com.w3codebox.service的包。
Étapes10: créer un paquet nommé BankService 的类。
Dans cette classe, nous définissons une classe nommée}} la méthode displayBalance(). Il vérifie le numéro de compte. Si le numéro de compte correspond, il retourne le solde total, sinon il retourne un message.
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("Solde total : "); 10,000"); } else { System.out.println("Désolé ! mauvais numéro de compte."); } } }
Étapes11: créer un autre paquet nommé com.w3codebox.aspect.
Étapes12: créer un paquet nommé BankAspect.
dans la classe suivante, nous définissons deux classes nommées logDisplayingBalance()et aroundAdvice()méthode.
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; //activer la fonction AOP de Spring dans l'application @Aspect @Component public class BankAspect { //afficher toutes les méthodes disponibles, qui notifie toutes les appels de méthode @Pointcut(value= "execution(* com.w3codebox.service.BankService.*(..))) private void logDisplayingBalance() { } //Declare the around notification applied before and after the method matches the切入点expression @Around(value= "logDisplayingBalance()") public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable { System.out.println("Before method call aroundAdvice() method" + jp.getSignature().getName() + " method "); try { jp.proceed(); } finally { } System.out.println("After method call aroundAdvice() method" + jp.getSignature().getName() + " method "); } }
Étapes13: Ouvrir AopAroundAdviceExampleApplication.java file and add annotations @EnableAspectJAutoProxy.
This annotation enables support for processing components marked with AspectJ @Aspect components annotated. It is used together with @Configuration annotation.
ConfigurableApplicationContext is an interface that provides tools for configuring the application context in addition to the application context client methods in 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 annotation support for processing components marked with @Aspect annotation. It is similar to the tag in xml configuration. @EnableAspectJAutoProxy public class AopAroundAdviceExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args); //Obtenir l'objet employee à partir du contexte de l'application. BankService bank = context.getBean(BankService.class); //Afficher le solde du compte String accnumber = "12345"; bank.displayBalance(accnumber); //Fermer l'objet context context.close(); } }
Après la création de tous les paquets et classes, le répertoire du projet est le suivant:
Maintenant, exécutez l'application.
Étapes14: Ouvrir AopAroundAdviceExampleApplication.java et l'exécuter comme application Java.
Dans la sortie ci-dessus, nous voyons que la méthode aroundAdvice() est appelée deux fois. Premièrement, en exécutant displayBalance()Avant la méthode, ensuite, en exécutant displayBalance()Après la méthode. Appelé conseil.