SpringBoot教程

SpringBoot AOP @Before

在面向方面的编程中使用建议之前,可以实现交叉操作。这是一种建议类型,可确保在方法执行之前运行建议。我们使用 @Before 注解来实现before通知。
让我们通过一个示例来理解before建议。

Spring Boot @Before示例

步骤1: 打开Spring Initializr http://start.spring.io 。
步骤2: 提供 Group 名称。我们提供了组名 com.lidihuo。
步骤3: 提供 Artifact Id 新建一个aop-before-advice-example。
步骤4: 添加 Spring Web 依赖性。
步骤5: 点击 Generate按钮。当我们单击"生成"按钮时,它将所有规范包装在 jar 文件中,并将其下载到本地系统。
Spring Boot AOP咨询之前
步骤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.lidihuo</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.lidihuo;
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.lidihuo.model包。
步骤11: 在包 com.lidihuo.model下创建一个类。 我们创建了一个名为 Employee的类。 在类中,定义以下内容:
定义三个String类型的变量 empId,firstName, secondName 生成Getters and Setters。 创建default
Employee.java
package com.lidihuo.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.lidihuo.controller的包。
步骤13: 在包 com.lidihuo.controller下创建一个控制器类。 我们创建了一个名为 EmployeeController的类。
在控制器类中,我们定义了两个映射,一个用于添加雇员,另一个用于删除雇员。
EmployeeController.java
package com.lidihuo.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.lidihuo.model.Employee;
import com.lidihuo.service.EmployeeService;
@RestController
public class EmployeeController 
{
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/add/employee", method = RequestMethod.GET)
public com.lidihuo.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.lidihuo.service的包。
步骤15: 在包 com.lidihuo.service下创建一个Service类。 我们创建了一个名为 EmployeeService的类。
在Service类中,我们定义了两个方法 createEmployee deleteEmployee。
EmployeeService .java
package com.lidihuo.service;
import org.springframework.stereotype.Service;
import com.lidihuo.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.lidihuo.aspect的包。
步骤17: 在包 com.lidihuo.aspect下创建一个方面类。 我们已经创建了一个名为 EmployeeServiceAspect的类。
在方面类中,我们定义了before通知逻辑。
EmployeeServiceAspect.java
package com.lidihuo.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 = "execution(* com.lidihuo.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);
}
}
在上面的类中:
执行(表达式): 表达式是一种可以应用建议的方法。 @Before: 它将功能标记为要在PointCut涵盖的方法之前执行的建议。
创建所有模块之后,项目目录如下所示:
Spring Boot AOP咨询之前
我们已经设置了所有模块。现在,我们将运行该应用程序。
第18步: 打开e AopBeforeAdviceExampleApplication.java 文件并将其作为Java应用程序运行。
步骤19: 打开浏览器并调用以下URL: http: //localhost: 8080/add/employee?empId = {id}&firstName = {fname}&secondName = {sname }
在上面的URL中, /add/employee 是我们在Controller类中创建的映射。我们使用了两个分隔符 (?)(&)来分隔两个值。
Spring Boot AOP在咨询之前
在上面的输出中,我们分配了 emId 101,firstName = Tim, secondName =做饭。
我们来看看控制台。我们看到,在调用 EmployeeService 类的 createEmployee ()方法之前, EmployeeServiceAspect 类的方法 beforeAdvice()如下所示。
在建议之前使用Spring Boot AOP
同样,我们也可以通过调用URL http://localhost:8080/remove/employee?empId = 101删除员工。它将返回一条消息 已撤职,如下图所示。
Spring Boot AOP咨询之前
在本节中,我们学习了咨询之前的工作。在下一部分中,我们将学习事后建议的工作并将其应用到实践中。


昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4