欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在Spring 中使用@Aspect 控制自定义注解的操作

程序员文章站 2022-03-10 23:43:57
spring 中使用@aspect 控制自定义注解看这篇介绍@aspect1.定义系统日志注解类@target(elementtype.method)@retention(retentionpolic...

spring 中使用@aspect 控制自定义注解

看这篇介绍@aspect

1.定义系统日志注解类

@target(elementtype.method)
@retention(retentionpolicy.runtime)
@documented
public @interface syslog {
string value() default "";
}

2.定义切面处理类

package com.kxs.common.aspect;
import com.google.gson.gson;
import com.kxs.common.annotation.syslog;
import com.kxs.common.utils.httpcontextutils;
import com.kxs.common.utils.iputils;
import com.kxs.modules.sys.entity.syslogentity;
import com.kxs.modules.sys.entity.sysuserentity;
import com.kxs.modules.sys.service.syslogservice;
import org.apache.shiro.securityutils;
import org.aspectj.lang.proceedingjoinpoint;
import org.aspectj.lang.annotation.around;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.pointcut;
import org.aspectj.lang.reflect.methodsignature;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;
import javax.servlet.http.httpservletrequest;
import java.lang.reflect.method;
import java.util.date;
/**
 * 系统日志,切面处理类
 * 
 * @author 
 * @email 
 * @date 
 */
@aspect
@component
public class syslogaspect {
 @autowired
 private syslogservice syslogservice;
 @pointcut("@annotation(com.kxs.common.annotation.syslog)")//指向自定义注解路径
 public void logpointcut() { 
 }
 /**
  * 切面记录系统日志
  * @param point
  * @return
  * @throws throwable
  */
 @around("logpointcut()")//
 public object around(proceedingjoinpoint point) throws throwable {
  long begintime = system.currenttimemillis();
  //执行方法
  object result = point.proceed();
  //执行时长(毫秒)
  long time = system.currenttimemillis() - begintime;
  //保存日志
  savesyslog(point, time);
  return result;
 }
//保存日志
 private void savesyslog(proceedingjoinpoint joinpoint, long time) {
  methodsignature signature = (methodsignature) joinpoint.getsignature();
  method method = signature.getmethod();
  syslogentity syslog = new syslogentity();
  syslog syslog = method.getannotation(syslog.class);
  if(syslog != null){
   //注解上的描述
   syslog.setoperation(syslog.value());
  }
  //请求的方法名
  string classname = joinpoint.gettarget().getclass().getname();
  string methodname = signature.getname();
  syslog.setmethod(classname + "." + methodname + "()");
  //请求的参数
  object[] args = joinpoint.getargs();
  try{
   string params = new gson().tojson(args[0]);
   syslog.setparams(params);
  }catch (exception e){
  }
  //获取request
  httpservletrequest request = httpcontextutils.gethttpservletrequest();
  //设置ip地址
  syslog.setip(iputils.getipaddr(request));
  //用户名
  string username = ((sysuserentity) securityutils.getsubject().getprincipal()).getusername();
  syslog.setusername(username);
  syslog.settime(time);
  syslog.setcreatedate(new date());
  //保存系统日志
  syslogservice.save(syslog);
 }
}

补充:为什么添加了@aspect 还要加@component

官方文档中有写:

you may register aspect classes as regular beans in your spring xml configuration, or autodetect them through classpath scanning - just like any other spring-managed bean. however, note that the @aspect annotation is not sufficient for autodetection in the classpath: for that purpose, you need to add a separate @component annotation (or alternatively a custom stereotype annotation that qualifies, as per the rules of spring's component scanner).

翻译:

您可以在spring xml配置中注册aspect类,或者通过类路径扫描自动检测它们,就像任何其他spring管理bean一样。但是,请注意,@aspect注释对于在类路径中自动检测是不够的:为了达到这个目的,您需要添加一个单独的@component注解(或者根据spring的组件扫描器的规则来定义一个定制的原型注解)。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。