中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用SpringBoot+AOP實現可插拔式日志的示例代碼

發布時間:2020-09-02 21:59:22 來源:腳本之家 閱讀:179 作者:Justin_Jun 欄目:編程語言

一、AOP

AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。 AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。

二、實現

引入依賴

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

新建SysLog類

package com.example.aop.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SysLog {
 String value() default " ";
}

新建SysLogAspect類

package com.example.aop.annotation;

import com.example.aop.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class SysLogAspect {
 @Autowired
 private LogService logService;

 @Pointcut("@annotation(com.example.aop.annotation.SysLog)")
 public void logPointCut() {}

 @Before("logPointCut()")
 public void before(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "before " + method.getName());
  } catch (Exception e) {
  }
 }

 @Around("logPointCut()")
 public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  Object object = joinPoint.proceed(args);
  try {
   logService.log(beginTime, "around " + method.getName());
  } catch (Exception e) {
  }
  return object;
 }

 @After("logPointCut()")
 public void after(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "after " + method.getName());
  } catch (Exception e) {
  }
 }

 @AfterReturning("logPointCut()")
 public void afterReturning(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "after returning " + method.getName());
  } catch (Exception e) {
  }
 }

 @AfterThrowing("logPointCut()")
 public void afterThrowing(JoinPoint joinPoint) {
  long beginTime = System.currentTimeMillis();
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLog annotation = method.getAnnotation(SysLog.class);
  String value = annotation.value();
  Object[] args = joinPoint.getArgs();
  try {
   logService.log(beginTime, "after throwing " + method.getName());
  } catch (Exception e) {
  }
 }
}

新建TestService類

package com.example.aop.service;

import com.example.aop.dto.TestDomain;
import com.example.aop.annotation.SysLog;
import org.springframework.stereotype.Service;

@Service
public class TestService {
 @SysLog("log-1")
 public void log1(String name, TestDomain testDomain) {
  System.out.println("print log 1" + name + " " + testDomain.toString());
 }

 @SysLog("log-2")
 public void log2(String name, TestDomain testDomain) {
  System.out.println("print log 2" + name + " " + testDomain.toString());
 }


 @SysLog("throw-exception")
 public void throwException() {
  System.out.println("throw exception");
  int i = 3/0;
 }
}

新建TestController

package com.example.aop.controller;

import com.example.aop.dto.TestDomain;
import com.example.aop.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
 @Autowired
 private TestService testService;

 @GetMapping("test")
 public String test(@RequestParam("p1") String p1) {
  TestDomain testDomain = new TestDomain();
  testDomain.setId("1");
  testDomain.setTitle("t1");
  testService.log1(p1, testDomain);
  testService.log2(p1, testDomain);
  testService.throwException();
  return "hello aop";
 }
}

三、測試

運行AopApplication, 然后訪問http://localhost:8080/test?p1=123,可以看到控制臺有以下輸出:

before log1 at: 1554903984403
print log 1123 com.example.aop.dto.TestDomain@2a7a4cd5
around log1 at: 1554903984402
after log1 at: 1554903984409
after returning log1 at: 1554903984409
before log2 at: 1554903984409
print log 2123 com.example.aop.dto.TestDomain@2a7a4cd5
around log2 at: 1554903984409
after log2 at: 1554903984410
after returning log2 at: 1554903984410
before throwException at: 1554903984410
throw exception
after throwException at: 1554903984410
after throwing throwException at: 1554903984410

github地址:https://github.com/lijun003/SpringBoot-AOP-log 

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

囊谦县| 和硕县| 龙游县| 绿春县| 高尔夫| 平阳县| 孟津县| 临海市| 宝山区| 迭部县| 武陟县| 五家渠市| 海口市| 苏尼特左旗| 壶关县| 年辖:市辖区| 二手房| 太仆寺旗| 钟祥市| 沈丘县| 潼南县| 吉木萨尔县| 连城县| 屯昌县| 金川县| 会宁县| 沁水县| 满洲里市| 朝阳市| 安徽省| 嘉兴市| 巴楚县| 临夏县| 鄱阳县| 岢岚县| 财经| 罗山县| 正镶白旗| 布尔津县| 汶川县| 玛纳斯县|