@AfterReturning 注释通常在连接点执行完成(执行)后调用。
步骤1 - 打开Spring Initializr http://start.spring.io 。
步骤2 - 提供Group名称。
步骤3 - 提供Artifact Id。
步骤4 - 添加 Spring Web 依赖项。
步骤5 - 单击 Generate (生成)按钮。当无涯教程单击"Generate"按钮时,它将所有规范包装在 jar 文件中,并将其下载到本地系统。
步骤6 - 提取下载的jar文件。
步骤7 - 通过以下步骤导入文件夹:
File -> Import -> Existing Maven Projects -> Next -> Browse the Folder aop-after-returning-advice-example -> Finish.
步骤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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.learnfk</groupId> <artifactId>aop-after-returning-advice-example</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>aop-after-returning-advice-example</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
步骤9 - 在 src/main/java 文件夹中创建名称为 com.learnfk.model 的软件包。
步骤10 - 在包 com.learnfk.model中创建一个名称为 Account 的类。
Account.java
package com.learnfk.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 + "]"; } }
步骤11 - 创建另一个名为 com.learnfk.service.impl的包。
步骤12 - 在此程序包中,创建一个名称为 AccountServiceImple的类。在此类中,无涯教程定义了AccountServer服务。
AccountServiceImpl.java
package com.learnfk.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.learnfk.model.Account; @Service public class AccountServiceImpl implements AccountService { //storing account detail in the HashMap private static Map<String,Account> map = null; static { map = new HashMap<>(); //adding account detail in the 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; } }
步骤13 - 在包 com.learnfk.service.impl。中创建一个名称为 AccountService 的接口。
AccountService.java
package com.learnfk.service.impl; import com.learnfk.model.Account; //creating interface that throws exception if the customer id not found public interface AccountService { public abstract Account getAccountByCustomerId(String customerId)throws Exception; }
步骤14 - 创建名称为 com.learnfk.aspect的程序包。
步骤15 - 在包 com.learnfk.aspect 中创建一个名称为 AccountAspect 的类。
在此类中,无涯教程通过使用注释 @AfterReturning实现了after return advice。 还定义了一种方法 afterReturningAdvice()方法。
AccountAspect.java
package com.learnfk.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.learnfk.model.Account; @Aspect @Component public class AccountAspect { //implementing after returning advice @AfterReturning(value="execution(* com.learnfk.service.impl.AccountServiceImpl.*(..))",returning="account") public void afterReturningAdvice(JoinPoint joinPoint, Account account) { System.out.println("After Returing method:"+joinPoint.getSignature()); System.out.println(account); } }
步骤16 - 打开 AopAfterReturningAdviceExampleApplication.java 文件,并添加注释 @EnableAspectJAutoProxy。
该注释支持对标有AspectJ的 @Aspect 注释的组件的支持。它与@Configuration注释一起使用。
无涯教程已使用注释@EnableAspectJAutoProxy的 proxyTargetClass 属性。属性 proxyTargetClass = true 允许使用 CGLIB (代码生成库)代理,而不是默认的基于接口的JDK代理方法。
ConfigurableApplicationContext 是一个接口,除了ApplicationContext中的应用程序上下文客户端方法外,提供了用于配置应用程序上下文工具。
AopAfterReturningAdviceExampleApplication.java
package com.learnfk; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.learnfk.model.Account; import com.learnfk.service.impl.AccountService; import com.learnfk.service.impl.AccountServiceImpl; @SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass=true) public class AopAfterReturningAdviceExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext ac = SpringApplication.run(AopAfterReturningAdviceExampleApplication.class, args); 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()); } } }
创建所有类和包之后,项目目录如下所示:
步骤17 - 打开AopAfterReturningAdviceExampleApplication.java文件并将其作为 Java 应用程序运行。它显示输出,如下所示:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)