个性化阅读
专注于IT技术分析

Spring Boot AOP After Advice用法详解

点击下载

在面向方面的编程中使用后Advice来实现交叉。这是一种Advice类型, 可确保Advice在方法执行后运行。我们使用@After注释来实现afterAdvice。

让我们通过示例了解Advice。

After Advice的Spring Boot示例

步骤1:打开Spring Initializr http://start.spring.io。

步骤2:提供群组名称。我们提供了组名com.srcmini。

第3步:提供工件ID。我们提供了”工件ID通知后aop”示例。

步骤4:添加Spring Web依赖项。

步骤5:点击Generate(生成)按钮。当我们单击Generate按钮时, 它将所有规范包装在jar文件中, 并将其下载到本地系统。

Spring Boot AOP After Advice用法详解

步骤6:解压缩下载的jar文件。

步骤7:使用以下步骤导入文件夹:

文件->导入->现有Maven项目->下一步->浏览文件夹aop-after-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-after-advice-example</artifactId>
<version>0.0.1-SNAPSHOT</version>  
<packaging>jar</packaging>	
<name>aop-after-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:打开AopAfterAdviceExampleApplication.java文件, 并添加注释@EnableAspectJAutoProxy。

@EnableAspectJAutoProxy(proxyTargetClass=true)

它支持处理标有AspectJ的@Aspect批注的组件。它与@Configuration批注一起使用。我们可以使用proxyTargetClass属性来控制代理的类型。其默认值为false。

AopAfterAdviceExampleApplication.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 AopAfterAdviceExampleApplication 
{
public static void main(String[] args) {
SpringApplication.run(AopAfterAdviceExampleApplication.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 = "/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 = "/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的类。

在方面类中, 我们定义了After Advice逻辑。

EmployeeServiceAspect.java

package com.srcmini.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class EmployeeServiceAspect 
{
@After(value = "execution(* com.srcmini.service.EmployeeService.*(..)) and args(empId, fname, sname)")
public void afterAdvice(JoinPoint joinPoint, String empId, String fname, String sname) {
System.out.println("After method:" + joinPoint.getSignature());
System.out.println("Creating Employee with first name - " + fname + ", second name - " + sname + " and id - " + empId);
}
}

在以上课程中:

  • Execution(expression):表达式是一种将Advice应用于其上的方法。
  • @After:用@After注释的方法在所有与切入点表达式匹配的方法之后执行。

创建所有模块后, 项目目录如下所示:

Spring Boot AOP After Advice用法详解

我们已经设置了所有模块。现在, 我们将运行该应用程序。

步骤18:打开AopAfterAdviceExampleApplication.java文件, 并将其作为Java应用程序运行。

步骤19:打开浏览器并调用以下URL:http:// localhost:8080 / add / employee?empId = {id}&firstName = {fname}&secondName = {sname}

在上面的URL中, / add / employee是我们在Controller类中创建的映射。我们使用两个分隔符(?)和(&)分隔两个值。

Spring Boot AOP After Advice用法详解

在上面的输出中, 我们分配了emId 102, firstName = Sachin和secondName = Bansal。

让我们看一下控制台。我们看到, 在调用EmployeeService类的createEmployee()方法之后, 将调用EmployeeServiceAspect类的afterAdvice()方法, 如下所示。

Spring Boot AOP After Advice用法详解

同样, 我们也可以通过调用URL http:// localhost:8080 / remove / employee?empId = 102来删除员工。它返回一条消息”已删除员工”, 如下图所示。

Spring Boot AOP After Advice用法详解

在本节中, 我们学习了事后Advice的工作。在下一节中, 我们将学习围绕Advice的工作方式。

After Advice下载AOP示例项目


赞(0)
未经允许不得转载:srcmini » Spring Boot AOP After Advice用法详解

评论 抢沙发

评论前必须登录!