@Around 注释用来连接点之前和之后执行。
步骤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-around-advice-example -> Finish.
步骤8 - 打开 pom.xml 文件,并添加以下 AOP 依赖项。它是使用 Spring AOP 和 AspectJ 进行面向方面编程的入门。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
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.learnfk</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.learnfk.service的软件包。
步骤10 - 在上面的包中创建一个名为 BankService 的类。
在此类中,无涯教程定义了一个名为 displayBalance()的方法。 它检查帐号。如果帐号匹配则返回总金额,否则返回一条消息。
BankService.java
package com.learnfk.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.learnfk.aspect的包。
步骤12 - 在上面的包中创建一个名为 BankAspect的类。
在下面的类中,无涯教程定义了两个名为 logDisplayingBalance()和 aroundAdvice()方法的方法。
BankAspect.java
package com.learnfk.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; //Enables the spring AOP functionality in an application @Aspect @Component public class BankAspect { @Pointcut(value= "execution(* com.learnfk.service.BankService.*(..))") private void logDisplayingBalance() { } @Around(value= "logDisplayingBalance()") public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable { System.out.println("The method aroundAdvice() before invokation of the method " + jp.getSignature().getName() + " method"); try { jp.proceed(); } finally {} System.out.println("The method aroundAdvice() after invokation of the method " + jp.getSignature().getName() + " method"); } }
步骤13 - 打开 AopAroundAdviceExampleApplication.java 文件并添加注释 @EnableAspectJAutoProxy。
该注释支持对标有AspectJ的 @Aspect 注释的组件的支持。它与@Configuration注释一起使用。
AopAroundAdviceExampleApplication.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.service.BankService; @SpringBootApplication @EnableAspectJAutoProxy public class AopAroundAdviceExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args); BankService bank = context.getBean(BankService.class); //Displaying balance in the account String accnumber = "12345"; bank.displayBalance(accnumber); //Closing the context object context.close(); } }
创建所有软件包和类之后,项目目录如下所示:
现在,运行该应用程序。
步骤14 - 打开AopAroundAdviceExampleApplication.java并将其作为Java应用程序运行。
在上面的输出中,无涯教程看到aroundAdvice()方法被调用了两次。首先,在执行 displayBalance()方法之前,其次,在执行 displayBalance()方法之后。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)