springboot使用AOP实现切面编程

springboot使用AOP实现切面编程

之前看日志里面有用到aop的技术所以想照着做一个过程如下

1创建一个@interface文件

package io.sirc.common.annotation;

import java.lang.annotation.*;

/**
 * 系统日志注解
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysQueryLog {

	String value() default "";
}

2创建一个调用它的类,注意其中地址指向io.sirc.common.annotation.SysQueryLog是前面那货的地址

package io.sirc.common.aspect;

import io.sirc.common.utils.HttpContextUtils;
import io.sirc.common.utils.IPUtils;
import io.sirc.common.utils.R;
import io.sirc.modules.sea.entity.GetDataResponse;
import io.sirc.modules.sys.entity.SysQueryLogEntity;
import io.sirc.modules.sys.entity.SysUserEntity;
import io.sirc.modules.sys.service.SysQueryLogService;
import io.sirc.modules.sys.service.SysUserService;
import net.sf.json.JSONObject;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


/**
 * 系统日志,切面处理类
 *
 */
@Aspect
@Component
public class SysQueryLogAspect {
	@Value("${isLocal}")
	private  boolean isLocal;

	@Autowired
	private SysQueryLogService sysQueryLogService;

    @Autowired
    private SysUserService sysUserService;
	
	@Pointcut("@annotation(io.sirc.common.annotation.SysQueryLog)")
	public void logPointCut() { 
		
	}
}

3.在SysQueryLogAspect 中添加方法,当然可以使用好几种标签@before,@after之类的  因为我现在需要的是进来之前判断参数中是否含有某些值 ,如果存在则中断不执行,执行方法后在执行日志插入操作,就使用@Around了,这样一下俩都能实现,

其中point是传进方法的参数,result是返回回去的值 ,point.proceed()是执行需要的接口,写在这上面的是调用接口之前执行的方法,写在下面的是执行之后的方法

@Around("logPointCut()")
	public GetDataResponse around(ProceedingJoinPoint point) throws Throwable {
        GetDataResponse result = new GetDataResponse();
        long beginTime = System.currentTimeMillis();
		//执行时长(毫秒)
		
		JSONObject jo = getParam(point);
        if(!isQueryLock(jo)) {
            result.setData(new R().ok().put("message","当前账号和ip已被锁定,请稍后再试。").toString());
            return result;
        }
        if(!isCompatible(jo)) {
            result.setData(new R().ok().put("message","当前账号和ip不匹配").toString());
            return result;
        }
        result= (GetDataResponse) point.proceed();
        long time = System.currentTimeMillis() - beginTime;
        saveSysQueryLog(jo, time);

		return result;
	}

4.在接口前面加上标签

@SysQueryLog
    public GetDataResponse getDataRequest(@RequestParam Map<String, Object> params) {
    //执行接口代码~~~~~~~~~~~~~~~~~
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/2213.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 从零开始学android编程之表格布局管理器(2-1)

    从零开始学android编程之表格布局管理器(2-1)表格布局管理器用TableLayout类表示,该类派生自LinearLayout类,所以TableLayout类也具有LinearLayout类的方法和属性。线性布局管理器LinearLayout将在其中的“组件群”进行横向或者纵向的一字排列。而表格布局管理器TableLayout主要将“组件群”进行表格式的排列,即将“组件群”排列成指定行数和指定列数。1在表格布局管理器中插入行在表

    2022年5月5日
    70
  • Java和MySQL数据类型对应一览

    Java和MySQL数据类型对应一览Table20.25.MySQLTypestoJavaTypesforResultSet.getObject()MySQLTypeNameReturnvalueofGetColumnClassNameReturnedasJavaClassBIT(1)(newinMySQL-5.0)BITjava.lang.Bool

    2022年6月3日
    131
  • flutter属于前端还是后端(kotlin比较flutter)

    尝试了网上多种库和教程都没办法互解,JAVA代码如下,求个大佬帮忙用dart(Flutter)实现一个可以互解的aes加密代码packagecom.example.lib;importjavax.crypto.Cipher;importjavax.crypto.spec.IvParameterSpec;importjavax.crypto.spec.SecretKeySpec;public…

    2022年4月9日
    255
  • 【移动端】手机界面的设计尺寸

    【移动端】手机界面的设计尺寸从设计方面来看,做手机界面设计的尺寸一般分为iPhone和Android两种设备。iPhone的分辨率设备 逻辑分辨率(point)(pt) 物理分辨率(pixel)(px) 屏幕尺寸 缩放因子(scale) 像素密度PPI 比例(近似) iPhone2G/3/3GS 320×480 320×480 3.5寸 @1x 163 2:3 iPhone4/4S 320×480 640..

    2022年6月21日
    28
  • C语言单链表的基本操作总结(增删改查)「建议收藏」

    C语言单链表的基本操作总结(增删改查)「建议收藏」1.链表概述  链表是一种常见的数据结构。它与常见的数组是不同的,使用数组时先要指定数组包含元素的个数,即为数组的长度,但是如果向这个数组中加入的元素超过了数组的大小时,便不能将内容全部保存。  链表这种存储方式,其元素个数是不受限定的,当进行添加元素的时候存储的个数就会随之改变。  链表一般有两种形式,有空头链表和无空头链表。  在链表中有一个头指针变量,这个指针变量保存一个地址,…

    2022年6月16日
    36
  • StringUtils工具类常用方法「建议收藏」

    StringUtils工具类常用方法「建议收藏」StringUtils类在操作字符串是安全的,不会报空指针异常,也正因此,在操作字符串时使用StringUtils相比使用原生的String会更加安全。一、判空StringUtils提供常用的判断空字符串有两个方法:isEmpty和isBlank,这两者的有何区别呢,直接看源码://isEmptypublicstaticbooleanisEmpty(Stringstr)…

    2022年6月11日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号