首页 > 其他分享 >自定义注解+面向切面 日志记录

自定义注解+面向切面 日志记录

时间:2023-01-05 09:55:22浏览次数:40  
标签:String 自定义 System 切面 org println import 日志 out

自定义注解

package com.example.spring.controller;

import java.lang.annotation.*;

@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogTest {
  String type() default "";
  String name() default "";
}

target选择注解可以放在类或者方法上面

retention 什么时候启动注解

切面

@Pointcut 切点,切面什么时候可以切入(切入什么方法)

@Around 环绕加强,有助于对操作前后都操作,必须加上process方法,否则业务代码无法运行

@After 业务跑完之后运行

@AfterThrow 跑完如果抛异常会运行

@AfterReturning(若目标方法无异常,执行@AfterReturning注解方法的业务逻辑)

简单实现

package com.example.spring.controller;

import jdk.nashorn.internal.runtime.logging.Logger;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Component
@Aspect
@Logger
public class AopTest {

  @Pointcut("@annotation(com.example.spring.controller.LogTest)")
  public void log(){}

  @Around("log()")
  public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
      MethodSignature signature = (MethodSignature) joinPoint.getSignature();
      Method method = signature.getMethod();
      String name = signature.getName();
      LogTest logTest = signature.getMethod().getAnnotation(LogTest.class);
      String name1 = logTest.name();
      String type = logTest.type();
      System.out.println("method:"+method);
      System.out.println("name:"+name);
      System.out.println("name1:"+name1);
      System.out.println("type:"+type);
      return joinPoint.proceed();
  }
  @Before("log()")
  public void before(JoinPoint joinPoint){
      ServletRequestAttributes signature = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      HttpServletRequest request = signature.getRequest();
      String ip = request.getRemoteHost();
      String remoteUser = request.getRemoteUser();
      System.out.println("ip:"+ip);
      System.out.println("user"+remoteUser);
  }

  @After("log()")
  public void after(JoinPoint point){
      System.out.println("跑完了");
  }


  @AfterThrowing("log()")
  public void after1(JoinPoint joinPoint){
      System.out.println("baocuole");
  }
}
 

标签:String,自定义,System,切面,org,println,import,日志,out
From: https://www.cnblogs.com/zzdd/p/17026689.html

相关文章

  • 用XML自定义Excel功能区
    一、XML编写<?xmlversion="1.0"encoding="utf-8"?><customUIxmlns="http://schemas.microsoft.com/office/2006/01/customui"><ribbonstartFromScratch="fal......
  • C# 处理实体类赋值(获取嵌套类型,支持list 自定义类型)
    publicstaticTAESEncrypt<T>(Tobj)whereT:class{if(obj==null){returnobj;}varproperties=typeof(T).GetProperties();foreach(System.Reflecti......
  • 6.日志
    1.日志工厂通过使用内置的日志工厂提供日志功能。  可以通过在MyBatis配置文件mybatis-config.xml里面添加一项setting来选择日志实现。2.STDOUT_LOGGING......
  • 日志信息和日志等级
     (1)日志级别:CRITICAL:严重错误ERROR:一般错误WARNING:警告INFO:一般信息DEBUG:调试信息默认的日志等级是DEBUG只要出现了DEBUG或者DEBUG以上等级的日志那么这些日......
  • 2022.1.4 营业日志
    感觉阳了之后没完全好啊,非常想睡觉。以后这个东西把三天放在一起吧,感觉每天都更有点多的(P3488[POI2009]LYZ-IceSkatesDescription给一张二分图,其中左边第\(i\)个点......
  • 告警日志中报错ORA-07445 kkqstcrf
    问题描述:数据库例行巡检时发现告警日志中报错ORA-07445kkqstcrf,如下所示:数据库:oracle11.2.0.1告警日志:SunDec2522:08:222022BeginautomaticSQLTuningAdvisorrun......
  • MongoDB数据的导出导入及日志分析
    一、远程连接导出报错超时mongodump-h10.110.63.150:27017-u'admin'-p'passwd!'--authenticationDatabaseflowtest--dbflowtest-o/home/mongod/bak>mongodump......
  • 查询服务器日志时的操作
      查看log.log日志文件(实时滚动刷新)tail-flog.log通用的查日志方式,使用less进入日志文件比如查看当前目录下的console.log文件lessconsole.log#查找某个......
  • awc/swing实现超链接的自定义控件
    1、awc/swing实现超链接的自定义控件publicclassHFLinkLabelextendsJLabel{ privatestaticfinallongserialVersionUID=-4533897048399372238L; publicSt......
  • 云redis自定义参数
    云redis自定义参数自定义的参数支持组合排列定义 参数说明支持版本disable-command-list设置禁用命令,用户可根据实际业务需要关闭某些时间复杂度高或危险程......