亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring自定義注解配置簡單日志示例

 更新時間:2023年05月11日 09:01:07   作者:Archie_java  
這篇文章主要介紹了Spring自定義注解配置簡單日志示例,注解可以增強我們的java代碼,同時利用反射技術(shù)可以擴充實現(xiàn)很多功能,它們被廣泛應(yīng)用于三大框架底層,需要的朋友可以參考下

java在jdk1.5中引入了注解,spring框架也正好把java注解發(fā)揮得淋漓盡致。

下面會講解Spring中自定義注解的簡單流程,其中會涉及到spring框架中的AOP(面向切面編程)相關(guān)概念。 不清楚java注解的,可以先了解java自定義注解:Java自定義注解

一、創(chuàng)建自定義注解

requestUrl 為我們自定義的一個參數(shù)

package com.sam.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyLog {
    String requestUrl();
}

二、解析注解

此處使用了spring的AOP(面向切面編程)特性

通過@Aspect注解使該類成為切面類

通過@Pointcut 指定切入點 ,這里指定的切入點為MyLog注解類型,也就是被@MyLog注解修飾的方法,進入該切入點。

  • @Before 前置通知:在某連接點之前執(zhí)行的通知,但這個通知不能阻止連接點之前的執(zhí)行流程(除非它拋出一個異常)。
  • @Around 環(huán)繞通知:可以實現(xiàn)方法執(zhí)行前后操作,需要在方法內(nèi)執(zhí)行point.proceed(); 并返回結(jié)果。
  • @AfterReturning 后置通知:在某連接點正常完成后執(zhí)行的通知:例如,一個方法沒有拋出任何異常,正常返回。
  • @AfterThrowing 異常通知:在方法拋出異常退出時執(zhí)行的通知。
  • @After 后置通知:在某連接點正常完成后執(zhí)行的通知:例如,一個方法沒有拋出任何異常,正常返回。
package com.sam.annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect //AOP 切面
@Component
public class MyLogAspect {
    //切入點
    @Pointcut(value = "@annotation(com.sam.annotation.MyLog)")
    private void pointcut() {
    }
    /**
     * 在方法執(zhí)行前后
     *
     * @param point
     * @param myLog
     * @return
     */
    @Around(value = "pointcut() && @annotation(myLog)")
    public Object around(ProceedingJoinPoint point, MyLog myLog) {
        System.out.println("++++執(zhí)行了around方法++++");
        String requestUrl = myLog.requestUrl();
        //攔截的類名
        Class clazz = point.getTarget().getClass();
        //攔截的方法
        Method method = ((MethodSignature) point.getSignature()).getMethod();
        System.out.println("執(zhí)行了 類:" + clazz + " 方法:" + method + " 自定義請求地址:" + requestUrl);
        try {
            return point.proceed(); //執(zhí)行程序
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return throwable.getMessage();
        }
    }
    /**
     * 方法執(zhí)行后
     *
     * @param joinPoint
     * @param myLog
     * @param result
     * @return
     */
    @AfterReturning(value = "pointcut() && @annotation(myLog)", returning = "result")
    public Object afterReturning(JoinPoint joinPoint, MyLog myLog, Object result) {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        HttpSession session = request.getSession();
        System.out.println("++++執(zhí)行了afterReturning方法++++");
        System.out.println("執(zhí)行結(jié)果:" + result);
        return result;
    }
    /**
     * 方法執(zhí)行后 并拋出異常
     *
     * @param joinPoint
     * @param myLog
     * @param ex
     */
    @AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) {
        System.out.println("++++執(zhí)行了afterThrowing方法++++");
        System.out.println("請求:" + myLog.requestUrl() + " 出現(xiàn)異常");
    }
}

三、使用自定義注解

在controller中直接使用注解@MyLog。(在service中也可以,但是在@Around中使用@annotation時,注解要寫在類上,不能寫在接口上

package com.sam.controller;
import com.sam.annotation.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/index")
public class IndexController {
    @MyLog(requestUrl = "/index請求")
    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "index";
    }
}

啟動項目,訪問 //localhost:8080/index 結(jié)果

++++執(zhí)行了around方法++++
執(zhí)行了 類:class com.yfs.controller.IndexController 方法:public java.lang.String com.yfs.controller.IndexController.index() 自定義請求地址:/index請求
++++執(zhí)行了afterReturning方法++++
執(zhí)行結(jié)果:index

以上講解了Spring實現(xiàn)自定義注解的簡單流程,需要做更多的自定義操作,則需要在切面類里面進行編程了,比如,記錄日志信息的操作,日志信息寫入數(shù)據(jù)庫等。到此這篇關(guān)于Spring自定義注解配置簡單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

到此這篇關(guān)于Spring自定義注解配置簡單日志示例的文章就介紹到這了,更多相關(guān)Spring自定義注解日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 教你怎么用IDEA快速生成注釋文檔

    教你怎么用IDEA快速生成注釋文檔

    這篇文章主要介紹了教你怎么用IDEA快速生成注釋文檔,文中有非常詳細的代碼示例,對正在學(xué)習(xí)IDEA操作的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 線程池中execute與submit的區(qū)別說明

    線程池中execute與submit的區(qū)別說明

    這篇文章主要介紹了線程池execute與submit的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)

    Mybatis分頁插件PageHelper的配置和簡單使用方法(推薦)

    在使用Java Spring開發(fā)的時候,Mybatis算是對數(shù)據(jù)庫操作的利器了。這篇文章主要介紹了Mybatis分頁插件PageHelper的配置和使用方法,需要的朋友可以參考下
    2017-12-12
  • SpringBoot實現(xiàn)文件上傳接口

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

    這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)文件上傳接口,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • spring security集成cas實現(xiàn)單點登錄過程

    spring security集成cas實現(xiàn)單點登錄過程

    這篇文章主要介紹了spring security集成cas實現(xiàn)單點登錄過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 詳談@Cacheable不起作用的原因:bean未序列化問題

    詳談@Cacheable不起作用的原因:bean未序列化問題

    這篇文章主要介紹了@Cacheable不起作用的原因:bean未序列化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java截取字符串的幾種常用方法

    Java截取字符串的幾種常用方法

    這篇文章主要給大家介紹了關(guān)于Java截取字符串的幾種常用方法,在Java編程語言中,String類提供了用于操作字符串的豐富方法,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2023-09-09
  • 基于springboot+vue實現(xiàn)垃圾分類管理系統(tǒng)

    基于springboot+vue實現(xiàn)垃圾分類管理系統(tǒng)

    這篇文章主要為大家詳細介紹了基于springboot+vue實現(xiàn)垃圾分類管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決

    SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決

    這篇文章主要介紹了SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring?boot集成redisson的最佳實踐示例

    spring?boot集成redisson的最佳實踐示例

    這篇文章主要為大家介紹了spring?boot集成redisson的最佳實踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03

最新評論