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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 虚拟机连接本地数据库

    虚拟机连接本地数据库我们在运行虚拟机上面的项目时,可能要用到本机的数据库,在使用过程中会遇到数据库拒绝访问的情况,这是因为在安装本地数据库时没有启动远程连接。使用两种方法来解决这种问题一、使用命令行模式  第一步先切换到MySQL的安装路径下面的bin目录(我的MySQL的安装路径为D:\MySQL\MySQLServer5.6\bin);第二步登录到mysql中连接本地数据库mysql…

    2022年5月31日
  • 同步FIFO和异步FIFO总结[通俗易懂]

    同步FIFO和异步FIFO总结[通俗易懂]文章目录1.FIFO简介2.使用场景3.分类4.FIFO的常见参数5.FIFO设计5.1空满标志生成5.2异步FIFO的设计还要注意跨时钟域问题5.3gray码如何判断空满`6.同步FIFO实现`7.异步FIFO实现8.对FIFO进行约束9.关于异步FIFO最小深度的计算9.1写时钟快于读时钟,写和读的过程中没有空闲周期9.2写时钟频率大于读时钟频率,但在读写的过程中存在空闲周期9.3写时钟慢于读时钟,且读写过程中没有空闲周期9.4写时钟频率小于读时钟频率,但读写过程中存在空

    2022年8月13日
  • Spring Cloud版本冲突(java.lang.NoClassDefFoundError:org/springframework/boot/Bootstrapper)

    Spring Cloud版本冲突(java.lang.NoClassDefFoundError:org/springframework/boot/Bootstrapper)springboot整合springcloud和springcloudalibaba版本导致的问题问题描述问题描述修改springboot版本后,启动springboot项目项目报错:java.lang.NoClassDefFoundError:org/springframework/boot/Bootstrapperjava.lang.NoClassDefFoundError:org/springframework/boot/Bootstrapper atjava.lang.ClassL

    2022年7月20日
  • Toast弹窗_toast 提示

    Toast弹窗_toast 提示js部分importvuefrom”vue”;//这里就是我们刚刚创建的那个静态组件importtoastComponentfrom”./toast.vue”;//把vue组件引进来//返回一个扩展实例构造器constToastConstructor=vue.extend(toastComponent);//定义弹出组件的函数接收2个参数,要显示…

    2022年9月24日
  • Adobe Dreamweaver的使用教程

    Adobe Dreamweaver的使用教程1.打开AdobeDreamweaver软件2.打开后的界面3.新建一个站点,保存网页文件,图片,视频等4.输入站点的名字和保存的路径5.新建html文件6.选择设计模式7.制作你所需要的结果8.保存网页,才能在浏览器中预览

    2022年6月12日

发表回复

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

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