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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring Boot 和 AOP > 原文: [https://howtodoinjava.com/spring-boot2/aop-aspectj/](https://howtodoinjava.com/spring-boot2/aop-aspectj/) 學習在 Spring Boot 應用程序中實現 AOP,并使用 [AspectJ](https://www.eclipse.org/aspectj/) 添加不同的 aop 建議,以支持諸如日志記錄,性能分析,緩存和事務管理之類的跨領域關注點。 > 閱讀更多: [Spring AOP 教程](https://howtodoinjava.com/spring-aop-tutorial/) ## 1\. 使用 Spring Boot 設置 AOP #### 1.1. Maven 在 Spring Boot 中設置 AOP 需要包括`spring-boot-starter-aop`依賴項。 它將`spring-aop`和`aspectjweaver`依賴項導入到應用程序中。 [入門 pom.xml](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-aop/pom.xml) `pom.xml` ```java <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` #### 1.2. 啟用/禁用自動配置 導入以上依賴項會觸發[`AopAutoConfiguration`](https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/aop/AopAutoConfiguration.html),這會使用`@EnableAspectJAutoProxy`注解啟用 AOP。 如果`application.properties`中的`spring.aop.auto = false`不激活自動配置。 `application.properties` ```java #spring.aop.auto = false //'false' disables the auto configuration ``` #### 1.3. 使用`@Aspect`創建切面 可以在 Spring 運行中使用注解`@Aspect`注解創建一個切面,并使用`@Component`注解在 bean 容器中注冊。 在切面類中,我們可以根據需要創建建議。 例如,下面的類在包`com.howtodoinjava.aop`中所有類內的方法上對建議應用。 它捕獲方法的開始時間和結束時間,并將總的方法執行時間記錄在日志文件中。 `LoggingAspect.java` ```java import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; @Aspect @Component public class LoggingAspect { private static final Logger LOGGER = LogManager.getLogger(LoggingAspect.class); @Around("execution(* com.howtodoinjava.aop..*(..)))") public Object profileAllMethods(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); //Get intercepted method details String className = methodSignature.getDeclaringType().getSimpleName(); String methodName = methodSignature.getName(); final StopWatch stopWatch = new StopWatch(); //Measure method execution time stopWatch.start(); Object result = proceedingJoinPoint.proceed(); stopWatch.stop(); //Log method execution time LOGGER.info("Execution time of " + className + "." + methodName + " " + ":: " + stopWatch.getTotalTimeMillis() + " ms"); return result; } } ``` #### 1.4. 示例 創建一個簡單的服務類來測試以上建議。 `DomainService.java` ```java @Service public class DomainService { public Object getDomainObjectById(Long id) { try { Thread.sleep(new Random().nextInt(2000)); } catch (InterruptedException e) { //do some logging } return id; } } ``` 創建一個測試類并執行給定的方法并注意日志。 `AopSpringBootTest.java` ```java import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class AopSpringBootTest { @Autowired private DomainService service; @Test public void testGetDomainObjectById() { service.getDomainObjectById(10L); } } ``` `Console` ```java 2019-11-07T21:02:58.390+0530 INFO Execution time of DomainService.getDomainObjectById :: 1145 ms ``` 如我們所見,AOP 建議已應用于服務方法。 ## 2\. 建議類型 在 Aspectj AOP 中有五種類型的建議。 1. [`@Before`](https://howtodoinjava.com/spring-aop/aspectj-before-annotation-example/) :在連接點之前執行的建議,但是它不能阻止執行流程前進到連接點(除非它引發異常)。 2. [`@AfterReturning`](https://howtodoinjava.com/spring-aop/aspectj-after-returning-annotation-example/) :連接點正常完成后要執行的建議。 3. [`@AfterThrowing`](https://howtodoinjava.com/spring-aop/aspectj-afterthrowing-annotation-example/) :如果方法因拋出異常而退出,則要執行的建議。 4. [`@After`](https://howtodoinjava.com/spring-aop/aspectj-after-annotation-example/) :無論連接點退出的方式如何(正常或異常返回),都將執行建議。 5. [`@Around`](https://howtodoinjava.com/spring-aop/aspectj-around-annotation-example/) :圍繞連接點的建議,例如方法調用。 ## 3\. 結論 使用 Spring 運行實現 AOP 只需很少的工作,并且一旦添加`spring-boot-starter-aop`依賴項,我們就準備添加切面。 每當我們想禁用 aop 自動配置時,我們都可以通過切換到 `false`輕松完成。 創建切面和建議就像添加一些注解一樣簡單,例如`@Aspect`,`@Around`等。 學習愉快! [`下載源碼`](https://github.com/lokeshgupta1981/SpringExamples/tree/master/aop)
                  <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>

                              哎呀哎呀视频在线观看