最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot實現(xiàn)基于aop的切面日志

 更新時間:2022年09月02日 15:10:25   作者:yhmoling  
這篇文章主要為大家詳細介紹了springboot實現(xiàn)基于aop的切面日志,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了springboot實現(xiàn)基于aop的切面日志的具體代碼,供大家參考,具體內容如下

通過aop的切面方式實現(xiàn)日志

通切面攔截所有指定包下的所有方法

@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspect1{
? ? Logger logger = LoggerFactory.getLogger(LogAspect.class);

? ??
/**
?* 攔截切點
?*/

? ? @Pointcut("execution(*xx.xx.controller.*.*(..))")
? ? private void logPointCut() {
? ? ? ? logger.info("進入注解攔截");

? ? }

? ? //前置通知,在方法之前通知
? ? @Before(value = "logPointCut()")
? ? public void before(JoinPoint jp) {
? ? ? ? logger.info("方法調用前:" + "類名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //后置通知
? ? @After(value = "logPointCut()")
? ? public void doAfter(JoinPoint jp) {
? ? ? ? logger.info("方法調用結束:" + "類名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //環(huán)繞通知
? ? @Around("logPointCut()")
? ? public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
? ? ? ? logger.info("方法開始調用》》》》》》");

? ? ? ? Object retVal = pjp.proceed();
? ? ? ? logger.info("方法調用結束》》》》》》");
? ? ? ? return retVal;
? ? }

? ? //返回通知
? ? @AfterReturning(pointcut = "logPointCut()")
? ? public void doAfterReturning(JoinPoint jp) {
? ? ? ? logger.info("寫入日志");


? ? }

? ? //異常通知
? ? @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
? ? public void doAfterThrowing(JoinPoint jp, Throwable ex) {
? ? ? ? logger.info("方法異常,異常信息:" + ex.getMessage());
? ? }
}

攔截自定義注解

定義一個日志注解,在所有要需要記錄的方法上加蓋注解即可被后續(xù)的aop攔截處理

代碼如下

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {

? ? /**
? ? ?* 日志主題
? ? ?*/
? ? public String title() default "";

? ? /**
? ? ?* 操作具體業(yè)務
? ? ?*/
? ? public String business() default "";


? ? /**
? ? ?* 是否保留請求參數(shù)
? ? ?*/
? ? public boolean isSaveRequestData() default false;
? ? /**
? ? * 日志的類別,主要用于日志的分開記錄和查詢
? ? **/
LogType logType() default LogType.LOGIN;
}

攔截切面的實現(xiàn)

@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspect {
? ? Logger logger = LoggerFactory.getLogger(LogAspect.class);
? ? @Autowired
? ? private ServiceDemo serviceDemo;


? ? /**
? ? ?* 攔截切點
? ? ?*/


? ? @Pointcut("@annotation(moling.evolution.demo.aop.annotation.Log)")
? ? private void logPointCut() {
? ? ??

? ? }

? ? //前置通知,在方法之前通知
? ? @Before(value = "logPointCut()")
? ? public void before(JoinPoint jp) {
? ? ? ? logger.info("方法調用前:" + "類名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //后置通知
? ? @After(value = "logPointCut()")
? ? public void doAfter(JoinPoint jp) {
? ? ? ? logger.info("方法參數(shù):{}", jp.getArgs());
? ? ? ? logger.info(" ?{} || {}", jp.getStaticPart().getId(), jp.getStaticPart().getSourceLocation());
? ? ? ? jp.getStaticPart().getId();

? ? ? ? logger.info("方法調用結束:" + "類名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

? ? //環(huán)繞通知
? ? @Around("logPointCut()")
? ? public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
? ? ? ? logger.info("方法開始調用》》》》》》");

? ? ? ? Object retVal = pjp.proceed();
? ? ? ? logger.info("方法調用結束》》》》》》");
? ? ? ? return retVal;
? ? }

? ? //返回通知
? ? @AfterReturning(pointcut = "logPointCut()", returning = "object")
? ? public void doAfterReturning(JoinPoint jp, Object object) {
? ? ? ? System.out.println("返回通知");
? ? ? ? Log log = null;
? ? ? ? try {
? ? ? ? ? ? log = getAnnotationLog(jp);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();

? ? ? ? }
? ? ? ? System.out.println(object);
? ? ? ? System.out.println(log);
? ? ? ? if (log != null) {
? ? ? ? ? ? logger.info(log.title());
? ? ? ? ? ? logger.info(log.business());
? ? ? ? ? ? logger.info(log.user());
? ? ? ? ? ? logger.info(log.isSaveRequestData() + "");
? ? ? ? } else {
? ? ? ? ? ? logger.error("獲取注解信息失敗");
? ? ? ? }
? ? ? ? serviceDemo.demo();

? ? }

? ? //異常通知
? ? @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
? ? public void doAfterThrowing(JoinPoint jp, Throwable ex) {
? ? ? ? logger.info("方法異常,異常信息:" + ex.getMessage());
? ? ? ? serviceDemo.error();
? ? }

? ? /**
? ? ?* 是否存在注解,如果存在就獲取
? ? ?*/


? ? private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
? ? ? ? Signature signature = joinPoint.getSignature();
? ? ? ? MethodSignature methodSignature = (MethodSignature) signature;
? ? ? ? Method method = methodSignature.getMethod();
? ? ? ? if (method != null) {
? ? ? ? ? ? return method.getAnnotation(Log.class);
? ? ? ? }
? ? ? ? return null;
? ? }
}

基于事件通知實現(xiàn)日志的記錄

  • 攔截切面的實現(xiàn)
@Aspect
@Component
@EnableAspectJAutoProxy
public class LogAspectContext {

? ? Logger logger = LoggerFactory.getLogger(LogAspectContext.class);
? ? @Autowired
? ? private ApplicationContext applicationContext;

? ? /**
? ? ?* 攔截切點
? ? ?*/
? ? @Pointcut("@annotation(moling.evolution.demo.aop.annotation.Log)")
? ? private void logPointCut() {
? ? ? ? logger.info("進入注解攔截");

? ? }


? ? //返回通知
? ? @AfterReturning(pointcut = "logPointCut()", returning = "object")
? ? public void doAfterReturning(JoinPoint jp, Object object) throws Exception {
? ? ? ? applicationContext.publishEvent(new LogSuccessEvent(jp, null));

? ? }

? ? //異常通知
? ? @AfterThrowing(pointcut = "logPointCut()", throwing = "ex")
? ? public void doAfterThrowing(JoinPoint jp, Throwable ex) {
? ? ? ? logger.info("方法異常,異常信息:" + ex.getMessage());
? ? ? ? applicationContext.publishEvent(new LogSuccessEvent(jp, ex));
? ? }

? ? /**
? ? ?* 是否存在注解,如果存在就獲取
? ? ?*/
? ? private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
? ? ? ? Signature signature = joinPoint.getSignature();
? ? ? ? MethodSignature methodSignature = (MethodSignature) signature;
? ? ? ? Method method = methodSignature.getMethod();
? ? ? ? if (method != null) {
? ? ? ? ? ? return method.getAnnotation(Log.class);
? ? ? ? }
? ? ? ? return null;
? ? }
}
@Slf4j
@Service
public class ApplicationListener implements org.springframework.context.ApplicationListener<LogSuccessEvent> {

? ? @Autowired
? ? private ServiceDemo demo;

? ? @Override
? ? public void onApplicationEvent(LogSuccessEvent event) {
? ? ? ? JoinPoint joinPoint = event.getJp();
? ? ? ? Throwable ex = event.getThrowable();
? ? ? ? if (ex == null) {
? ? ? ? ? ? demo.demo();
? ? ? ? } else {
? ? ? ? ? ? demo.error();
? ? ? ? }
? ? }


}
@Slf4j
public class LogSuccessEvent extends ApplicationEvent {
? ? /**
? ? ?* Create a new ApplicationEvent.
? ? ?*
? ? ?* @param source the component that published the event (never {@code null})
? ? ?*/
? ? private JoinPoint jp;

? ? private Throwable throwable;

? ? public LogSuccessEvent(Object source, Throwable throwable) {
? ? ? ? super(source);
? ? ? ? this.throwable = throwable;
? ? ? ? this.jp = (JoinPoint) source;
// ? ? ? ?Log logger = (Log) source;
// ? ? ? ?log.info(logger.title());
// ? ? ? ?log.info(logger.business());
// ? ? ? ?log.info(logger.user());
// ? ? ? log.info(logger.isSaveRequestData() + "");

? ? }

? ? public JoinPoint getJp() {
? ? ? ? return jp;
? ? }

? ? public Throwable getThrowable() {
? ? ? ? return throwable;
? ? }
}

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

相關文章

  • MyBatis中RowBounds實現(xiàn)內存分頁

    MyBatis中RowBounds實現(xiàn)內存分頁

    RowBounds是MyBatis提供的一種內存分頁方式,適用于小數(shù)據(jù)量的分頁場景,本文就來詳細的介紹一下,具有一定的參考價值,感興趣的可以了解一下
    2024-12-12
  • JavaWeb中web.xml初始化加載順序詳解

    JavaWeb中web.xml初始化加載順序詳解

    本篇文章主要介紹了JavaWeb中web.xml初始化加載順序詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 帶你了解Java Maven的打包操作

    帶你了解Java Maven的打包操作

    這篇文章主要介紹了Maven打包的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • java基礎--自己動手實現(xiàn)一個LRU

    java基礎--自己動手實現(xiàn)一個LRU

    這篇文章主要介紹了運用方案如何實現(xiàn)LUR,文章中通過代碼講解的非常詳細,對大家的工作或學習有一定的參考價值,感興趣的朋友可以參考一下
    2021-08-08
  • java封裝及四種權限修飾符詳解

    java封裝及四種權限修飾符詳解

    這篇文章主要介紹了java封裝及四種權限修飾符詳解,對屬性進行封裝,使用戶不能直接輸入數(shù)據(jù),我們需要避免用戶再使用"對象.屬性"的方式對屬性進行賦值
    2022-08-08
  • 在SpringBoot中使用YourKit進行性能調優(yōu)的教程詳解

    在SpringBoot中使用YourKit進行性能調優(yōu)的教程詳解

    在應用程序的開發(fā)過程中,性能調優(yōu)是一個重要的環(huán)節(jié),在SpringBoot應用程序中,我們可以使用YourKit來進行性能調優(yōu),YourKit是一款非常強大的Java性能調優(yōu)工具,在本文中,我們將介紹如何在 SpringBoot應用程序中使用YourKit進行性能調優(yōu)
    2023-06-06
  • 使用log4j輸出一個類的所有參數(shù)的值

    使用log4j輸出一個類的所有參數(shù)的值

    這篇文章主要介紹了使用log4j輸出一個類的所有參數(shù)的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring MVC的項目準備和連接建立方法

    Spring MVC的項目準備和連接建立方法

    SpringWebMVC是基于Servlet API的Web框架,屬于Spring框架的一部分,主要用于簡化Web應用程序的開發(fā),SpringMVC通過控制器接收請求,使用模型處理數(shù)據(jù),并通過視圖展示結果,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • HarmonyOS實現(xiàn)Java端類似Nine-Patch氣泡聊天框代碼

    HarmonyOS實現(xiàn)Java端類似Nine-Patch氣泡聊天框代碼

    在HarmonyOS Java端實現(xiàn)氣泡聊天框,與Android 上的9圖(Nine-Patch)有相似的實現(xiàn)方式,在HarmonyOS中,可以使用ShapeElement和ElementContainer來創(chuàng)建和管理可伸縮的氣泡背景,下面提供一個簡單的示例代碼,可以在 HarmonyOS 中實現(xiàn)類似于Android的Nine-Patch氣泡聊天框效果
    2024-07-07
  • springBoot整合redis做緩存具體操作步驟

    springBoot整合redis做緩存具體操作步驟

    緩存主要是將數(shù)據(jù)存在計算機的內存當中,以便于在使用的時候是可以實現(xiàn)快速讀取使用,它的快也是相對于硬盤讀取而言,這篇文章主要給大家介紹了關于springBoot整合redis做緩存的具體操作步驟,需要的朋友可以參考下
    2024-04-04

最新評論

丰顺县| 定边县| 商都县| 鹤壁市| 霍林郭勒市| 祁连县| 永宁县| 永宁县| 普宁市| 开远市| 平和县| 衡阳市| 星子县| 苏尼特左旗| 潞西市| 岳阳县| 徐水县| 墨脱县| 根河市| 股票| 永州市| 汤原县| 惠来县| 高安市| 山东| 洛扎县| 焉耆| 宿迁市| 花垣县| 崇明县| 陇西县| 石屏县| 黄龙县| 满洲里市| 攀枝花市| 香河县| 瑞安市| 蒲城县| 扎兰屯市| 颍上县| 新宾|