<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國際加速解決方案。 廣告
                ## 引入AOP依賴 在Spring Boot中引入AOP就跟引入其他模塊一樣,非常簡單,只需要在`pom.xml`中加入如下依賴: ~~~ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ~~~ * 實現一個簡單請求處理:通過傳入name參數,返回“hello xxx”的功能 ~~~ @RestController public class HelloController { @RequestMapping("/hello") public String hello(@RequestParam("name") String name){ return "Hello"+name; } } ~~~ ## 實現Web層的日志切面 實現AOP的切面主要有以下幾個要素: * 使用`@Aspect`注解將一個java類定義為切面類 * 使用`@Pointcut`定義一個切入點,可以是一個規則表達式,比如下例中某個package下的所有函數,也可以是一個注解等。 * 根據需要在切入點不同位置的切入內容 * 使用`@Before`在切入點開始處切入內容 * 使用`@After`在切入點結尾處切入內容 * 使用`@AfterReturning`在切入點return內容之后切入內容(可以用來對處理返回值做一些加工處理) * 使用`@Around`在切入點前后切入內容,并自己控制何時執行切入點自身的內容 * 使用`@AfterThrowing`用來處理當切入內容部分拋出異常之后的處理邏輯 ~~~ /** * 使用aop前置通知攔截請求參數信息 * @author Administrator * */ @Aspect @Component public class WebLogAspect { private static final Logger logger=LoggerFactory.getLogger(WebLogAspect.class); @Pointcut("execution(public * cn.li.controller.*.*(..))") public void webLog() { } @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 接收到請求,記錄請求內容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 記錄下請求內容 logger.info("URL : " + request.getRequestURL().toString()); logger.info("HTTP_METHOD : " + request.getMethod()); logger.info("IP : " + request.getRemoteAddr()); logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 處理完請求,返回內容 logger.info("RESPONSE : " + ret); } } ~~~ 通過運行程序并訪問:`http://localhost:8080/hello?name=didi`,可以獲得下面的日志輸出 ~~~ 2019-01-07 18:40:44.050 INFO 5940 --- [nio-8080-exec-4] cn.li.aop.WebLogAspect : URL : http://localhost:8080/hello 2019-01-07 18:40:44.051 INFO 5940 --- [nio-8080-exec-4] cn.li.aop.WebLogAspect : HTTP_METHOD : GET 2019-01-07 18:40:44.051 INFO 5940 --- [nio-8080-exec-4] cn.li.aop.WebLogAspect : IP : 0:0:0:0:0:0:0:1 2019-01-07 18:40:44.054 INFO 5940 --- [nio-8080-exec-4] cn.li.aop.WebLogAspect : CLASS_METHOD : cn.li.controller.HelloController.hello 2019-01-07 18:40:44.054 INFO 5940 --- [nio-8080-exec-4] cn.li.aop.WebLogAspect : ARGS : [zhangsan] 2019-01-07 18:40:44.061 INFO 5940 --- [nio-8080-exec-4] cn.li.aop.WebLogAspect : RESPONSE : Hellozhangsan ~~~
                  <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>

                              哎呀哎呀视频在线观看