Spring Boot AOP Before Advice用法详解

在面向方面的编程中使用Before Advice, 要实现交叉。这是一种Advice 类型, 可确保Advice 在方法执行之前运行。我们使用@Before注释来实现beforeAdvice 。
让我们先通过示例了解Advice 。
Before Advice的Spring Boot示例 步骤1:打开Spring Initializr http://start.spring.io。
步骤2:提供群组名称。我们提供了组名com.srcmini。
第3步:提供工件ID。我们提供了” Before Advice aop实例” ID。
步骤4:添加Spring Web依赖项。
步骤5:点击Generate(生成)按钮。当我们单击Generate按钮时, 它将所有规范包装在一个jar文件中, 并将其下载到本地系统。

Spring Boot AOP Before Advice用法详解

文章图片
步骤6:解压缩下载的jar文件。
步骤7:使用以下步骤导入文件夹:
文件-> 导入-> 现有Maven项目-> 下一步-> 浏览文件夹aop-before-advice-example-> 完成。
步骤8:打开pom.xml文件并添加以下AOP依赖项。它是使用Spring AOP和AspectJ进行面向方面的编程的入门。
< dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-aop< /artifactId> < /dependency> < /dependencies>

pom.xml
< 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.srcmini< /groupId> < artifactId> aop-before-advice-example< /artifactId> < version> 0.0.1-SNAPSHOT< /version> < packaging> jar< /packaging> < name> aop-before-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-web< /artifactId> < /dependency> < dependency> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-starter-aop< /artifactId> < /dependency> < /dependencies> < build> < plugins> < plugin> < groupId> org.springframework.boot< /groupId> < artifactId> spring-boot-maven-plugin< /artifactId> < /plugin> < /plugins> < /build> < /project>

步骤9:打开AopBeforeAdviceExampleApplication.java文件, 并添加注释@EnableAspectJAutoProxy。
@EnableAspectJAutoProxy(proxyTargetClass=true)

它支持处理标有AspectJ的@Aspect注释的组件。它与@Configuration批注一起使用。我们可以使用proxyTargetClass属性来控制代理的类型。其默认值为false。
AopBeforeAdviceExampleApplication.java
package com.srcmini; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass=true) public class AopBeforeAdviceExampleApplication { public static void main(String[] args) { SpringApplication.run(AopBeforeAdviceExampleApplication.class, args); } }

步骤10:创建一个名称为com.srcmini.model的包。
步骤11:在com.srcmini.model包下创建一个模型类。我们创建了一个名为Employee的类。在该类中, 定义以下内容:
  • 定义三个类型为String的变量empId, firstName和secondName。
  • 生成Getter和Setter。
  • 创建一个默认值
Employee.java
package com.srcmini.model; public class Employee { private String empId; private String firstName; private String secondName; //default constructor public Employee() { } public String getEmpId() { return empId; } public void setEmpId(String empId) { this.empId = empId; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getSecondName() { return secondName; } public void setSecondName(String secondName) { this.secondName = secondName; } }

步骤12:创建一个名称为com.srcmini.controller的包。
步骤13:在com.srcmini.controller包下创建一个控制器类。我们创建了一个名为EmployeeController的类。
在控制器类中, 我们定义了两个映射, 一个用于添加雇员, 另一个用于删除雇员。
EmployeeController.java
package com.srcmini.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.srcmini.model.Employee; import com.srcmini.service.EmployeeService; @RestController public class EmployeeController { @Autowired private EmployeeService employeeService; @RequestMapping(value = "http://www.srcmini.com/add/employee", method = RequestMethod.GET) public com.srcmini.model.Employee addEmployee(@RequestParam("empId") String empId, @RequestParam("firstName") String firstName, @RequestParam("secondName") String secondName) { return employeeService.createEmployee(empId, firstName, secondName); } @RequestMapping(value = "http://www.srcmini.com/remove/employee", method = RequestMethod.GET) public String removeEmployee( @RequestParam("empId") String empId) { employeeService.deleteEmployee(empId); return "Employee removed"; } }

步骤14:创建一个名称为com.srcmini.service的包。
步骤15:在com.srcmini.service包下创建一个Service类。我们创建了一个名为EmployeeService的类。
在Service类中, 我们定义了两个方法createEmployee和deleteEmployee。
EmployeeService.java
package com.srcmini.service; import org.springframework.stereotype.Service; import com.srcmini.model.Employee; @Service public class EmployeeService { public Employee createEmployee( String empId, String fname, String sname) { Employee emp = new Employee(); emp.setEmpId(empId); emp.setFirstName(fname); emp.setSecondName(sname); return emp; } public void deleteEmployee(String empId) { } }

步骤16:创建一个名称为com.srcmini.aspect的包。
步骤17:在com.srcmini.aspect包下创建一个方面类。我们创建了一个名为EmployeeServiceAspect的类。
在方面类中, 我们定义了事前通知逻辑。
EmployeeServiceAspect.java
package com.srcmini.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class EmployeeServiceAspect { @Before(value = "http://www.srcmini.com/execution(* com.srcmini.service.EmployeeService.*(..)) and args(empId, fname, sname)") public void beforeAdvice(JoinPoint joinPoint, String empId, String fname, String sname) { System.out.println("Before method:" + joinPoint.getSignature()); System.out.println("Creating Employee with first name - " + fname + ", second name - " + sname + " and id - " + empId); } }

在以上课程中:
  • 执行(表达式):表达式是一种将Advice 应用于其上的方法。
  • @Before:将功能标记为要在PointCut覆盖的方法之前执行的Advice 。
创建所有模块后, 项目目录如下所示:
Spring Boot AOP Before Advice用法详解

文章图片
我们已经设置了所有模块。现在, 我们将运行该应用程序。
步骤18:打开eAopBeforeAdviceExampleApplication.java文件并将其作为Java应用程序运行。
步骤19:打开浏览器并调用以下URL:http:// localhost:8080 / add / employee?empId = {id}&firstName = {fname}&secondName = {sname}
在上面的URL中, / add / employee是我们在Controller类中创建的映射。我们使用两个分隔符(?)和(&)分隔两个值。
Spring Boot AOP Before Advice用法详解

文章图片
在上面的输出中, 我们分配了emId 101, firstName = Tim和secondName = cook。
【Spring Boot AOP Before Advice用法详解】让我们看一下控制台。我们看到, 在调用EmployeeService类的createEmployee()方法之前, 将调用EmployeeServiceAspect类的beforeAdvice()方法, 如下所示。
Spring Boot AOP Before Advice用法详解

文章图片
同样, 我们也可以通过调用URL http:// localhost:8080 / remove / employee?empId = 101来删除员工。它返回一条消息” 已删除员工” , 如下图所示。
Spring Boot AOP Before Advice用法详解

文章图片
在本节中, 我们学习了事前Advice 的工作。在下一节中, 我们将学习事后Advice 的工作并将其应用于附件中。
在Before Advice下载AOP示例项目

    推荐阅读