<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring AOP 事后建議示例 > 原文: [https://howtodoinjava.com/spring-aop/aspectj-after-advice-example/](https://howtodoinjava.com/spring-aop/aspectj-after-advice-example/) 在這個 Spring aop 示例中,我們將學習使用`<aop:after/>`配置來使用 AOP 事后建議。 配置為事后建議的方法,在與作為參數傳遞的切入點表達式匹配的方法之后立即運行。 在此示例中,我們將創建簡單的 spring 應用程序,添加日志記錄切面,然后基于在`<aop:after/>` xml 配置中傳遞的切入點信息來調用切面方法。 ## 創建 Spring AOP 事后建議 要使用 xml 配置創建后續建議,請按以下方式使用`<aop:after/>`。 ```java <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut expression="execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))" id="loggingPointcuts"/> <!-- after advice --> <aop:after method="logAfterAllMethods" pointcut-ref="loggingPointcuts" /> </aop:aspect> </aop:config> ``` ## 項目結構 ![Spring AOP Project Structure](https://img.kancloud.cn/e5/86/e586f19859a2c6a8c2b0abf70ea319bc_398x468.jpg) Spring AOP 項目結構 ## Spring AOP AspectJ Maven 依賴關系 我添加了 spring 核心,spring aop 和 Aspectj 依賴項。 ```java <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.howtodoinjava</groupId> <artifactId>SpringAOPExamples</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Spring AOP Examples</name> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> </dependencies> </project> ``` ## 添加 Spring AOP 配置 在 XML 配置文件中,您可以添加`aop:config`元素以添加 AOP 支持。 ```java <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:config> <aop:aspect ref="loggingAspect"> <aop:pointcut expression="execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))" id="loggingPointcuts"/> <!-- after advice --> <aop:after method="logAfterAllMethods" pointcut-ref="loggingPointcuts" /> </aop:aspect> </aop:config> <!-- Employee manager --> <bean id="employeeManager" class="com.howtodoinjava.app.service.impl.EmployeeManagerImpl" /> <!-- Logging Aspect --> <bean id="loggingAspect" class="com.howtodoinjava.app.aspect.LoggingAspect" /> </beans> ``` ## 需要執行切面的服務方法 `EmployeeManager.java`和`EmployeeManagerImpl.java` ```java public interface EmployeeManager { public EmployeeDTO getEmployeeById(Integer employeeId); public List<EmployeeDTO> getAllEmployee(); public void createEmployee(EmployeeDTO employee); public void deleteEmployee(Integer employeeId); public void updateEmployee(EmployeeDTO employee); } public class EmployeeManagerImpl implements EmployeeManager { public EmployeeDTO getEmployeeById(Integer employeeId) { System.out.println("Method getEmployeeById() called"); return new EmployeeDTO(); } public List<EmployeeDTO> getAllEmployee() { System.out.println("Method getAllEmployee() called"); return new ArrayList<EmployeeDTO>(); } public void createEmployee(EmployeeDTO employee) { System.out.println("Method createEmployee() called"); } public void deleteEmployee(Integer employeeId) { System.out.println("Method deleteEmployee() called"); } public void updateEmployee(EmployeeDTO employee) { System.out.println("Method updateEmployee() called"); } } ``` ## 編寫切面類和方法 編寫切面類和要作為建議執行的方法。 ```java package com.howtodoinjava.app.aspect; import org.aspectj.lang.JoinPoint; public class LoggingAspect { public void logAfterAllMethods(JoinPoint jp) throws Throwable { System.out.println("****LoggingAspect.logBeforeAllMethods() " + jp.getSignature().getName()); } } ``` ## 測試 Spring AspectJ 的配置和執行 現在,我們來測試以上配置的切面是否在給定的切入點信息上執行。 ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.howtodoinjava.app.model.EmployeeDTO; import com.howtodoinjava.app.service.EmployeeManager; public class TestMain { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); EmployeeManager manager = (EmployeeManager) context.getBean("employeeManager"); manager.getEmployeeById(1); manager.createEmployee(new EmployeeDTO()); } } ``` ```java Method getEmployeeById() called ****LoggingAspect.logBeforeAllMethods() getEmployeeById Method createEmployee() called ****LoggingAspect.logBeforeAllMethods() createEmployee ``` 明確切面建議在相關連接點上執行。 學習愉快! 參考文獻: [Spring AOP 參考](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html) [AspectJ 項目](https://eclipse.org/aspectj/) [不同的切入點表達式以及示例](https://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with-examples/)
                  <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>

                              哎呀哎呀视频在线观看