iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring自定义注解配置简单日志示例
  • 106
分享到

Spring自定义注解配置简单日志示例

Spring自定义注解Spring日志注解 2023-05-19 20:05:47 106人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录一、创建自定义注解二、解析注解三、使用自定义注解java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致。 下面会讲解Spring中自定义注解的简单

java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致。

下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的aop(面向切面编程)相关概念。 不清楚java注解的,可以先了解java自定义注解:Java自定义注解

一、创建自定义注解

requestUrl 为我们自定义的一个参数

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 前置通知:在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
  • @Around 环绕通知:可以实现方法执行前后操作,需要在方法内执行point.proceed(); 并返回结果。
  • @AfterReturning 后置通知:在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
  • @AfterThrowing 异常通知:在方法抛出异常退出时执行的通知。
  • @After 后置通知:在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
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() {
    }
    
    @Around(value = "pointcut() && @annotation(myLog)")
    public Object around(ProceedingJoinPoint point, MyLog myLog) {
        System.out.println("++++执行了around方法++++");
        String requestUrl = myLog.requestUrl();
        //拦截的类名
        Class clazz = point.getTarget().getClass();
        //拦截的方法
        Method method = ((MethodSignature) point.getSignature()).getMethod();
        System.out.println("执行了 类:" + clazz + " 方法:" + method + " 自定义请求地址:" + requestUrl);
        try {
            return point.proceed(); //执行程序
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return throwable.getMessage();
        }
    }
    
    @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("++++执行了afterReturning方法++++");
        System.out.println("执行结果:" + result);
        return result;
    }
    
    @AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) {
        System.out.println("++++执行了afterThrowing方法++++");
        System.out.println("请求:" + myLog.requestUrl() + " 出现异常");
    }
}

三、使用自定义注解

在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 结果

++++执行了around方法++++
执行了 类:class com.yfs.controller.IndexController 方法:public java.lang.String com.yfs.controller.IndexController.index() 自定义请求地址:/index请求
++++执行了afterReturning方法++++
执行结果:index

以上讲解了Spring实现自定义注解的简单流程,需要做更多的自定义操作,则需要在切面类里面进行编程了,比如,记录日志信息的操作,日志信息写入数据库等。到此这篇关于Spring自定义注解配置简单日志示例的文章就介绍到这了,更多相关Spring自定义注解日志内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

到此这篇关于Spring自定义注解配置简单日志示例的文章就介绍到这了,更多相关Spring自定义注解日志内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring自定义注解配置简单日志示例

本文链接: https://www.lsjlt.com/news/213686.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作