欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用SpringBoot+AOP實現(xiàn)可插拔式日志的示例代碼

 更新時間:2019年07月26日 16:19:49   作者:Justin_Jun  
這篇文章主要介紹了使用SpringBoot+AOP實現(xiàn)可插拔式日志的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一、AOP

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

二、實現(xiàn)

引入依賴

  <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 

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

相關文章

  • 一文帶你掌握Java?SPI的原理和實踐

    一文帶你掌握Java?SPI的原理和實踐

    在Java中,我們經(jīng)常會提到面向接口編程,這樣減少了模塊之間的耦合,更加靈活,Java?SPI?(Service?Provider?Interface)就提供了這樣的機制,本文就來講講它的原理與具體使用吧
    2023-05-05
  • 基于java的opencv開發(fā)過程詳解

    基于java的opencv開發(fā)過程詳解

    這篇文章主要介紹了基于java的opencv開發(fā)過程詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • java開發(fā)Activiti進階篇流程實例詳解

    java開發(fā)Activiti進階篇流程實例詳解

    這篇文章主要為大家介紹了java開發(fā)Activiti進階篇流程實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Java中的HashMap集合深度解析

    Java中的HashMap集合深度解析

    這篇文章主要介紹了Java中的HashMap集合深度解析, HashMap可以說是Java中最常用的集合類框架之一,是Java語言中非常典型的數(shù)據(jù)結構,我們總會在不經(jīng)意間用到它,很大程度上方便了我們日常開發(fā),需要的朋友可以參考下
    2023-09-09
  • SpringBoot實現(xiàn)文件上傳接口

    SpringBoot實現(xiàn)文件上傳接口

    這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)文件上傳接口,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • java中優(yōu)化大量if...else...方法總結

    java中優(yōu)化大量if...else...方法總結

    在我們平時的開發(fā)過程中,經(jīng)??赡軙霈F(xiàn)大量If else的場景,代碼顯的很臃腫,非常不優(yōu)雅,下面這篇文章主要給大家介紹了關于java中優(yōu)化大量if...else...方法的相關資料,需要的朋友可以參考下
    2023-03-03
  • java基于C/S模式實現(xiàn)聊天程序(服務器)

    java基于C/S模式實現(xiàn)聊天程序(服務器)

    這篇文章主要為大家詳細介紹了java基于C/S模式實現(xiàn)聊天程序的服務器篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • java虛擬機學習筆記基礎篇

    java虛擬機學習筆記基礎篇

    在本篇文章里小編給大家整理的是關于java虛擬機學習筆記的相關內容,分享了一些基礎知識點,需要的朋友們參考下。
    2019-06-06
  • 詳解Java變量與常量

    詳解Java變量與常量

    這篇文章主要介紹了Java變量與常量,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Java Spring boot 2.0 跨域問題的解決

    Java Spring boot 2.0 跨域問題的解決

    本篇文章主要介紹了Java Spring boot 2.0 跨域問題的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論