如何在 Spring Boot 應用程式中實現 AOP?


AOP 是一種強大的技術,用於在軟體開發中模組化和處理橫切關注點。在 Spring Boot 應用程式中,AOP 允許將應用程式邏輯與安全、快取、事務管理和日誌記錄等任務分離。將 AOP 應用於您的專案涉及最初配置依賴項,然後透過註釋或 XML 配置定義方面。

這些方面包含在特定條件下適用的各種功能。此外,可以配置切點以確定在使用 AOP 的 Spring Boot 應用程式的程式碼庫中應將方面應用於何處。這種方法促進了程式碼模組化、可重用性和可維護性,同時保持關注點分離並易於管理。

Spring Boot

Spring Boot 是一個免費的基於 Java 的框架,簡化了功能強大的應用程式的開發。它構建在廣泛支援的 Spring Framework 之上,使開發人員能夠建立設定最少的 Java 應用程式。Spring Boot 的基礎是其對約定優於配置程式設計理念的關注;這意味著開發人員可以快速部署,而無需大量樣板程式碼。

此外,它擁有眾多庫和工具來幫助構建應用程式,包括嵌入式 Web 伺服器、資料庫訪問、安全性和日誌記錄功能等。因此,Spring Boot 使開發人員能夠透過利用其固有的預設值和自動配置功能來輕鬆快速擴充套件任何型別的應用程式。

方法

在 Spring Boot 應用程式中實現面向方面程式設計 (AOP) 的方法有很多。以下是一些常用的方法

  • 使用 Spring AOP 註解

  • 使用 XML 配置

  • 使用 Spring Boot 啟動器依賴項

使用 Spring AOP 註解

此方法涉及透過 @EnableAspectJAutoProxy 註解啟用 AOP,建立一個帶有 @Aspect 註解的方面類,使用 @Pointcut 定義切點表示式,並使用 @Before、@After 或 @Around 等註解實現通知方法。然後,方面使用 @Autowired 注入到 Spring 元件中。

演算法

  • 在應用程式中啟用 AOP。

  • 使用 @Aspect 註解建立一個方面類。

  • 定義切點表示式以指定應將方面應用於何處。

  • 使用 @Before、@After 或 @Around 等註解實現通知方法。

  • 使用 @Autowired 將方面注入到 Spring 元件中。

示例

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@EnableSpringConfigured
@SpringBootApplication
public class MyApp {
   public static void main(String[] args) {
      SpringApplication.run(MyApp.class, args);
      
      MyService service = new MyService();
      service.doSomething();
   }
}
@Aspect
@Component
class LoggingAspect {

   @Pointcut("execution(* MyService.*(..))")
   public void serviceMethods() {}

   @Before("serviceMethods()")
   public void beforeServiceMethod() {
      System.out.println("Before executing a service method.");
   }
}

@Service
class MyService {
   public void doSomething() {
      System.out.println("Doing something in MyService.");
   }
}

輸出

Before executing a service method.
Doing something in MyService.

使用 XML 配置

透過這種方法,透過定義 XML 配置檔案在 Spring Boot 應用程式中配置 AOP。使用 <aop:aspectj-autoproxy/> 元素啟用 AOP,使用所需的類宣告方面 bean,使用 <aop:pointcut> 定義切點表示式,並使用 <aop:before>、<aop:after> 或 <aop:around< 實現通知方法。然後,方面 bean 用於應用程式中。

演算法

  • 使用 XML 配置檔案在應用程式中配置 AOP。

  • 使用 <aop:aspectj-autoproxy/< 元素啟用 AOP。

  • 使用所需的類宣告方面 bean。

  • 使用 <aop:pointcut< 定義切點表示式。

  • 使用 <aop:before<、<aop:after< 或 <aop:around< 在方面 bean 中實現通知方法。

示例

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@EnableSpringConfigured
@SpringBootApplication
public class MyApp {
   public static void main(String[] args) {
      SpringApplication.run(MyApp.class, args);
      
      MyService service = new MyService();
      service.doSomething();
   }
}

@Service
class MyService {
   public void doSomething() {
      System.out.println("Doing something in MyService.");
   }
}

@Aspect
@Component
class LoggingAspect {

   @Pointcut("execution(* MyService.*(..))")
   public void serviceMethods() {}

   @Before("serviceMethods()")
   public void beforeServiceMethod() {
      System.out.println("Before executing a service method.");
   }
}

輸出

Before executing a service method.
Doing something in MyService.

使用 Spring Boot 啟動器依賴項

在此方法中,您將必要的 Spring Boot 啟動器依賴項新增到您的專案中。您建立一個用 @Aspect 註釋的方面類,使用 @Pointcut 定義切點表示式,並使用 @Before、@After 或 @Around 等註解實現通知方法。Spring Boot 自動檢測並將方面應用於應用程式中的適當連線點,無需任何其他配置。

演算法

  • 將必要的 Spring Boot 啟動器依賴項新增到專案中。

  • 建立一個用 @Aspect 註釋的方面類。

  • 使用 @Pointcut 定義切點表示式。

  • 使用 @Before、@After 或 @Around 等註解實現通知方法。

  • Spring Boot 自動檢測並將方面應用於應用程式中的適當連線點。

示例

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

@EnableSpringConfigured
@SpringBootApplication
public class MyApp {
   public static void main(String[] args) {
      SpringApplication.run(MyApp.class, args);
      
      MyService service = new MyService();
      service.doSomething();
   }
}

@Service
class MyService {
   public void doSomething() {
      System.out.println("Doing something in MyService.");
   }
}

@Aspect
@Component
class LoggingAspect {

   @Pointcut("execution(* MyService.*(..))")
   public void serviceMethods() {}

   @Before("serviceMethods()")
   public void beforeServiceMethod() {
      System.out.println("Before executing a service method.");
   }
}

輸出

Before executing a service method.
Doing something in MyService.

結論

在本教程中,AOP(面向方面程式設計)提供了一種強大的方法來模組化 Spring Boot 應用程式中的橫切關注點。透過使用註釋、XML 配置或 Spring Boot 啟動器依賴項,開發人員可以輕鬆地實現 AOP 以分離和管理日誌記錄、安全性和效能等關注點,從而產生更簡潔、更易於維護的程式碼。AOP 有助於實現更好的程式碼可重用性、關注點分離和改進的應用程式可擴充套件性。

更新於: 2023-07-25

2K+ 閱讀量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告