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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring AOP AspectJ `@Around`注解示例 > 原文: [https://howtodoinjava.com/spring-aop/aspectj-around-annotation-example/](https://howtodoinjava.com/spring-aop/aspectj-around-annotation-example/) 在此 **Spring 操作示例**中,我們將學習使用 **Aspectj `@Around`注解**。 `@Around`帶注解的方法在與切入點表達式匹配的所有方法之前和之后運行。 在此示例中,我們將創建一個簡單的 spring 應用程序,添加記錄切面周圍的內容,然后基于`@Around`注解中傳遞的切入點信息調用切面方法。 ## 1\. AspectJ `@Around`注解用法 `@Around`建議**包圍了連接點**,例如方法調用。 這是最有力的建議。 圍繞建議可以在方法調用之前和之后執行自定義行為。 它還負責選擇是返回連接點還是通過返回其自身的返回值或引發異常來捷徑建議的方法執行。 ```java @Aspect public class LoggingAspect { @Around("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAroundAllMethods(ProceedingJoinPoint joinPoint) { ... } @Around("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.getEmployeeById(..))") public void logAroundGetEmployee(ProceedingJoinPoint joinPoint) { ... } } ``` 不要忘記使用`ProceedingJoinPoint`作為參數。 您必須調用`ProceedingJoinPoint.proceed()`方法,否則將執行原始方法。 ## 2\. 項目結構 ![Spring AOP Project Structure](https://img.kancloud.cn/e5/86/e586f19859a2c6a8c2b0abf70ea319bc_398x468.jpg) Spring AOP 項目結構 ## 3\. 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> ``` ## 4\. 啟用 Spring AOP AspectJ 支持 在 XML 配置文件中,您可以添加`aop:aspectj-autoproxy`元素以啟用`@AspectJ`注解支持。 ```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 "> <!-- Enable @AspectJ annotation support --> <aop:aspectj-autoproxy /> <!-- Employee manager --> <bean id="employeeManager" class="com.howtodoinjava.app.service.impl.EmployeeManagerImpl" /> <!-- Logging Aspect --> <bean id="loggingAspect" class="com.howtodoinjava.app.aspect.LoggingAspect" /> </beans> ``` ## 5\. 需要執行`@Around`切面的方法 `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"); } } ``` ## 6\. 編寫 AspectJ 注解的類和方法 用切入點信息編寫 aspectj 注解的類和方法。 ```java @Aspect public class LoggingAspect { @Around("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.*(..))") public void logAroundAllMethods(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("****LoggingAspect.logAroundAllMethods() : " + joinPoint.getSignature().getName() + ": Before Method Execution"); try { joinPoint.proceed(); } finally { //Do Something useful, If you have } System.out.println("****LoggingAspect.logAroundAllMethods() : " + joinPoint.getSignature().getName() + ": After Method Execution"); } @Around("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.getEmployeeById(..))") public void logAroundGetEmployee(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("****LoggingAspect.logAroundGetEmployee() : " + joinPoint.getSignature().getName() + ": Before Method Execution"); try { joinPoint.proceed(); } finally { //Do Something useful, If you have } System.out.println("****LoggingAspect.logAroundGetEmployee() : " + joinPoint.getSignature().getName() + ": After Method Execution"); } @Around("execution(* com.howtodoinjava.app.service.impl.EmployeeManagerImpl.createEmployee(..))") public void logAroundCreateEmployee(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("****LoggingAspect.logAroundCreateEmployee() : " + joinPoint.getSignature().getName() + ": Before Method Execution"); try { joinPoint.proceed(); } finally { //Do Something useful, If you have } System.out.println("****LoggingAspect.logAroundCreateEmployee() : " + joinPoint.getSignature().getName() + ": After Method Execution"); } } ``` ## 7\. Spring AspectJ `@Around`示例 現在,我們來測試以上配置的切面是否在給定的切入點信息上執行。 ```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 ****LoggingAspect.logAroundAllMethods() : getEmployeeById: Before Method Execution ****LoggingAspect.logAroundGetEmployee() : getEmployeeById: Before Method Execution Method getEmployeeById() called ****LoggingAspect.logAroundGetEmployee() : getEmployeeById: After Method Execution ****LoggingAspect.logAroundAllMethods() : getEmployeeById: After Method Execution ****LoggingAspect.logAroundAllMethods() : createEmployee: Before Method Execution ****LoggingAspect.logAroundCreateEmployee() : createEmployee: Before Method Execution Method createEmployee() called ****LoggingAspect.logAroundCreateEmployee() : createEmployee: After Method Execution ****LoggingAspect.logAroundAllMethods() : createEmployee: After Method Execution ``` 顯然,在相關連接點周圍執行切面建議。 學習愉快! 參考文獻: [Spring AOP 參考](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html) [`@Around`注解](https://eclipse.org/aspectj/doc/next/aspectj5rt-api/org/aspectj/lang/annotation/Around.html) [`@Aspect`注解](https://eclipse.org/aspectj/doc/next/aspectj5rt-api/org/aspectj/lang/annotation/Aspect.html) [AspectJ 注解配置示例](https://howtodoinjava.com/spring/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/) [不同切入點表達式以及示例](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>

                              哎呀哎呀视频在线观看