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

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

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

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

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

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

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

? ??
/**
?* 攔截切點(diǎn)
?*/

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

? ? }

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

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

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

? ? ? ? Object retVal = pjp.proceed();
? ? ? ? logger.info("方法調(diào)用結(jié)束》》》》》》");
? ? ? ? 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());
? ? }
}

攔截自定義注解

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

代碼如下

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

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

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


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

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

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


? ? /**
? ? ?* 攔截切點(diǎn)
? ? ?*/


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

? ? }

? ? //前置通知,在方法之前通知
? ? @Before(value = "logPointCut()")
? ? public void before(JoinPoint jp) {
? ? ? ? logger.info("方法調(diào)用前:" + "類名:" + 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("方法調(diào)用結(jié)束:" + "類名:" + jp.getTarget().getClass().getName() + "方法名 :" + jp.getSignature().getName());
? ? }

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

? ? ? ? Object retVal = pjp.proceed();
? ? ? ? logger.info("方法調(diào)用結(jié)束》》》》》》");
? ? ? ? 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;
? ? }
}

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

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

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

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

? ? }


? ? //返回通知
? ? @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;
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis中RowBounds實(shí)現(xiàn)內(nèi)存分頁(yè)

    MyBatis中RowBounds實(shí)現(xiàn)內(nèi)存分頁(yè)

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

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

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

    帶你了解Java Maven的打包操作

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

    java基礎(chǔ)--自己動(dòng)手實(shí)現(xiàn)一個(gè)LRU

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

    java封裝及四種權(quán)限修飾符詳解

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

    在SpringBoot中使用YourKit進(jìn)行性能調(diào)優(yōu)的教程詳解

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

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

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

    Spring MVC的項(xiàng)目準(zhǔn)備和連接建立方法

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

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

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

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

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

最新評(píng)論

嫩江县| 扎兰屯市| 遂川县| 黔江区| 扬中市| 贺兰县| 偏关县| 兴业县| 凤城市| 麦盖提县| 阳山县| 屏山县| 灯塔市| 宜城市| 德惠市| 阳山县| 沙田区| 甘孜县| 富蕴县| 屯昌县| 林甸县| 阳春市| 依安县| 辉县市| 文水县| 抚远县| 福建省| 山阴县| 介休市| 乌兰察布市| 老河口市| 丹东市| 霍山县| 常山县| 嘉祥县| 瑞丽市| 安康市| 富平县| 屏山县| 武鸣县| 扶绥县|