English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
After returning是Spring AOP中的一个建议,通常在连接点执行完成(执行)后调用。如果引发异常,则不会调用。我们可以使用 @AfterReturning 在应用程序中返回建议后实施。注解将功能标记为要在PointCut覆盖的方法之前执行的建议。
在返回的建议运行之后,当匹配的方法执行并返回正常值时,将执行该建议。我们在return属性中定义的名称必须与advice方法中的参数名称相对应。当方法返回值时,该值将作为相应的参数值传递到通知方法。
让我们在应用程序中实现返回通知。
Schritte1: 打开Spring Initializr http://start.spring.io 。
Schritte2: 提供 Group 名称。我们提供了组名 com.w3codebox。
Schritte3: 提供了 Artifact Id。提供Artifact Id aop-nach-zurückgebend-Ratschlag-example。
Schritte4: 添加 Spring Web 依赖项。
Schritte5: 点击 Generieren按钮。当我们点击"生成"按钮时,它将所有规范包装在 jar Datei und laden Sie sie auf das lokale System herunter.
Schritte6: Extrahieren
第7Schritt: Importieren Sie die folgenden SchritteOrdner:
Datei->Importieren->Bestehendes Maven-Projekt->Nächste->Browsen Sie den Ordner aop-zurückgebend-Ratschlag-example Beispiel->Erledigt.
Schritte8: Öffnen pom.xml Datei und fügen Sie die folgenden AOP Abhängigkeiten. Es wird Spring AOP und AspectJ Einführung in die aspektorientierte Programmierung.
<Abhängigkeit> <groupId>org.springframework.boot</groupId>/<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-Instanz 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> <Gruppen-ID>com.w3codebox</<groupId> <Artikelnummer>aop-nach-zurückgebend-Ratschlag-Beispiel</> artifactId <Version>0.0.1-SNAPSHOT</version> <Verpackung>jar</Verpackung> <name>aop-nach-zurückgebend-Ratschlag-Beispiel</name> <Beschreibung>Demo-Projekt für Spring Boot</Beschreibung> <parent> <groupId>org.springframework.boot</groupId>/<groupId> <artifactId>spring-boot-Starter-parent</> artifactId <version>2.2.2.RELEASE</version> <relativePath/> <!-- suche das Parent aus dem Repository --> </parent> <Eigenschaften> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </Eigenschaften> <Abhängigkeiten> <Abhängigkeit> <groupId>org.springframework.boot</groupId>/<groupId> <artifactId>spring-boot-Starter-aop</> artifactId </<dependency> <Abhängigkeit> <groupId>org.springframework.boot</groupId>/<groupId> <artifactId>spring-boot-Starter-<test<//> artifactId <scope>test</scope>/<scope> </<dependency> </<dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>/<groupId> <artifactId>spring-boot-maven-<plugin/> artifactId </> Plugin </> Plugins </> Bau </> Projekt
Schritte9: 在 src/main/java Verzeichnis Erstellen Sie in com.w3codebox.model Paket.
Schritte10: im Paket com.w3Erstellen Sie in codebox.model einen Namen Account von der Klasse.
Führen Sie in der "Account"-Klasse die folgenden Aktionen aus:
definierten zwei Variablen vom Typ String accountNumber und accountType . Klicken Sie mit der rechten Maustaste auf die Datei-> Quelle-> Erstellen Sie einen Konstruktor aus den Feldern > Generierung von Getters.
Klicken Sie mit der rechten Maustaste auf die Datei-> Quelle-> Generieren Sie Getter und Setter-> Wählen Sie Getter-> Generieren Generieren toString()
Klicken Sie mit der rechten Maustaste auf die Datei-> Quelle-> Generierung von 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 + "]"; } }
Schritte11: Erstellen Sie ein weiteres Namens com.w3Paket von codebox.service.impl.
Schritte12: 在此程序包中,创建一个名称为 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中添加帐户详细信息 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; } }
Schritte13: 在 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; }
Schritte14: Erstellen eines Namens com.w3codebox.aspect-Paket.
Schritte15: im Paket com.w3codebox.aspect eine mit dem Namen AccountAspect von der Klasse.
In dieser Klasse verwenden wir die Annotation @AfterReturning. Wir haben auch afterReturningAdvice()Methode.
Hinweis: Wir beachten: definierten name(account) > returning die Eigenschaft muss mit advice-Methodekorrespondieren. Die Parametername
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("Nach Rückgabe des Methoden:")+joinPoint.getSignature()); System.out.println(account); } }
Schritte16: Öffnen AopAfterReturningAdviceExampleApplication.java Datei hinzufügen und Annotation hinzufügen @EnableAspectJAutoProxy.
Annotationunterstützung zur Verarbeitung von AspectJ-beschlossenen @Aspect Annotationen-Komponenten. Es wird zusammen mit der @Configuration-Annotation verwendet.
Wir verwenden die @EnableAspectJAutoProxy-Annotation. proxyTargetClass Eigenschaft. Eigenschaft proxyTargetClass = true ermöglicht es uns, CGLIB (Codegenerierungsbibliothek) Proxy anstatt der standardmäßigen JDK-Proxy-Methode, die auf Basis von Interfaces basiert.
ConfigurableApplicationContext ist eine Schnittstelle, die neben den ApplicationContext-Anwendungscontext-Clientmethoden auch Werkzeuge zur Konfiguration des Anwendungscontexts bereitstellt.
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-Objekt aus der Anwendungsumgebung holen 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()); } } }
Nachdem alle Klassen und Pakete erstellt wurden, sieht der Projektverzeichnis wie folgt aus:
Schritte17: Öffnen AopAfterReturningAdviceExampleApplication.java Datei und als Java-Anwendung ausführen. Es zeigt die Ausgabe wie folgt:
In dem nächsten Abschnitt werden wir nach Vorschlägen verstehen.