<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring AOP 教程示例 > 原文: [https://howtodoinjava.com/spring-aop-tutorial/](https://howtodoinjava.com/spring-aop-tutorial/) 在本 **Spring AOP 教程**中,通過示例學習什么是面向切面的編程。 還通過示例了解什么是建議,連接點和切入點表達式以及如何在 Spring 應用程序中使用它們。 ## 1\. Spring AOP – 簡介 Spring AOP 支持在 Spring 應用程序中進行面向切面的編程。 在 AOP 中,各個切面可以實現關注點的模塊化,例如事務管理,日志記錄或跨多個類型和對象的安全性(通常稱為**跨切點關注點**)。 AOP 提供了一種使用簡單的可插拔配置在實際邏輯之前,之后或周圍動態添加橫切關注點的方法。 現在和現在,維護代碼都很容易。 您只需更改配置文件即可添加/刪除關注點,而無需重新編譯完整的源代碼(如果您要使用要求 XML 配置的切面)。 ## 2\. 什么是建議,連接點和切入點 1. AOP 中的一個重要術語是**建議**。 它是**切面**在特定連接點處采取的操作。 2. **連接點**是程序的執行點,例如方法的執行或異常的處理。 在 Spring AOP 中,連接點始終代表方法的執行。 3. **切入點**是與連接點匹配的謂詞或表達式。 4. **建議**與切入點表達式關聯,并在與該切入點匹配的任何連接點處運行。 5. Spring 默認使用 AspectJ 切入點表達語言。 ![Spring AOP](https://img.kancloud.cn/b0/ea/b0ea6db74223087a001891731df99d5b_410x372.jpg) Spring AOP ## 3\. AOP 建議的類型 Spring AOP 中有五種建議。 1. **事前建議**:在連接點之前執行的建議,但是它不能阻止執行流程前進到連接點(除非它拋出異常)。 2. **返回后建議**:在連接點正常完成后要執行的建議:例如,如果某個方法返回而沒有拋出異常。 3. **拋出后建議**:如果方法因拋出異常而退出,則要執行的建議。 4. **事后建議**:無論連接點退出的方式如何(正常或特殊返回),都將執行建議。 5. **周圍建議**:圍繞連接點的建議,例如方法調用。 這是最有力的建議。 周圍建議可以在方法調用之前和之后執行自定義行為。 它還負責選擇是返回連接點還是通過返回其自身的返回值或拋出異常來捷徑建議的方法執行。 ## 4\. Spring AOP 示例 #### 4.1. Maven 依賴 在編寫任何代碼之前,您將需要將 Spring AOP 依賴項導入到您的項目中。 `pom.xml` ```java <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> ``` #### 4.2. 切面和切入點表達式 編寫使用`@Aspect`注解注解的切面類,并編寫切入點表達式以匹配連接點方法。 `EmployeeCRUDAspect.java` ```java @Aspect public class EmployeeCRUDAspect { @Before("execution(* EmployeeManager.getEmployeeById(..))") //point-cut expression public void logBeforeV1(JoinPoint joinPoint) { System.out.println("EmployeeCRUDAspect.logBeforeV1() : " + joinPoint.getSignature().getName()); } } ``` #### 4.3. 方法(連接點) 編寫要在其上執行建議并與切入點表達式匹配的方法。 `EmployeeManager.java` ```java @Component public class EmployeeManager { public EmployeeDTO getEmployeeById(Integer employeeId) { System.out.println("Method getEmployeeById() called"); return new EmployeeDTO(); } } ``` 在上面的示例中,`logBeforeV1()`將在 `getEmployeeById()`方法之前執行,**因為它與連接點表達式匹配**。 #### 4.4. 運行應用程序 運行該應用程序并查看控制臺。 `TestAOP.java` ```java ublic class TestAOP { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext ("com/howtodoinjava/demo/aop/applicationContext.xml"); EmployeeManager manager = context.getBean(EmployeeManager.class); manager.getEmployeeById(1); } } ``` 程序輸出: `Console` ```java EmployeeCRUDAspect.logBeforeV1() : getEmployeeById Method getEmployeeById() called ``` Spring aop 初學者教程,并帶有示例。 ## 5\. Spring AOP XML 配置示例 1. [**Spring AOP AspectJ XML 配置示例**](https://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-xml-configuration-example/) 學習使用 XML 配置來配置 AOP 切面。 2. [**Spring AOP 事前建議示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-before-advice-example/) 學習使用`<aop:before/>`配置事前建議 AOP 切面。 3. [**Spring AOP 返回后建議示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-after-returning-advice-example/) 學習使用`<aop:after-returning/>`配置返回后建議 AOP 切面。 4. [**Spring AOP 拋出后建議示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-after-throwing-advice-example/) 學習使用`<aop:after-throwing/>`配置拋出后建議 AOP 切面。 5. [**Spring AOP 事后建議示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-after-advice-example/) 學習使用`<aop:after/>`配置事后建議 AOP 切面。 6. [**Spring AOP 周圍建議示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-around-advice-example/) 學習使用`<aop:around/>`配置圍繞建議 AOP 切面。 ## 6\. Spring AOP AspectJ 注解示例 1. [**Spring AOP AspectJ 注解配置示例**](https://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/) 學習使用 aspectj 注解配置來配置 AOP 切面。 2. [**Spring AOP AspectJ `@Before`示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-before-annotation-example/) 學習使用`@Before`注解配置事前建議 AOP 切面。 3. [**Spring AOP AspectJ `@After`示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-after-annotation-example/) 學習使用`@After`注解配置事后建議 AOP 切面。 4. [**Spring AOP AspectJ `@Around`示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-around-annotation-example/) 學習使用`@Around`注解圍繞建議 AOP 切面。 5. [**Spring AOP AspectJ `@AfterReturning`示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-after-returning-annotation-example/) 學習使用`@AfterReturning`注解返回后建議 AOP 切面。 6. [**Spring AOP AspectJ `@AfterThrowing`示例**](https://howtodoinjava.com/spring/spring-aop/aspectj-afterthrowing-annotation-example) 學習使用`@AfterThrowing`注解拋出后建議 AOP 切面。 ## 7\. 更多 Spring AOP 教程 1. [**Spring AOP Aspects 排序**](https://howtodoinjava.com/spring/spring-aop/spring-aop-specifying-aspects-ordering/) 在需要按特定順序執行的多個切面中,學習對切面執行進行排序。 2. [**Spring AOP AspectJ 切入點表達式示例**](https://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/) 學習編寫切點表達式以匹配各種連接點。 ## 8\. 面試題 [**熱門 Spring AOP 面試問題與答案**](https://howtodoinjava.com/spring/spring-aop/top-spring-aop-interview-questions-with-answers/) Java 面試中一些最常問到的 Spring AOP 面試問題。 ## 9\. Spring AOP 資源: [Spring AOP 文檔](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html) [AspectJ](https://eclipse.org/aspectj/) 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看