这篇博客主要写给自己看的,方便以后回顾

AspectJ是一个AOP框架,由于SpringAOP的配置过于繁琐,因此使用了AspectJ依赖注解开发

1、Aspecj依赖坐标,此处省略了Spring相关依赖

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.7.1</version>
</dependency>

2、在applicationContext.xml文件中配上

<aop:aspectj-autoproxy/>

代表使用Aspecj的注解开发模式

3、自定义创建的切入点,以及要实现的功能

package com.util;import com.dao.LogDAO;import com.entity.LogMessage;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;
@Aspect     // 用来声明这是一个AspectJ
@Component  // 在此处声明一个Component 是因为Spring扫描注解并不能识别AspectJ 因此在此处声明,不必在applicationContext.xml配置bean标签了
public class ServiceLog {@Autowired   private LogDAO logDAO;//在该无参无内容的方法上添加一个@Poincut()来声明切入点,在后来的@Around("pc()")直接写入该方法的名字就能在自动使用这些切点@Pointcut("execution(* com.service..*.add*(..)) || execution(* com.service..*.modify*(..)) || execution(* com.service..*.drop*(..))")public void pc() {}//环绕通知注解@Around("pc()")public Object log(ProceedingJoinPoint pjp) throws Throwable {// 获取requestHttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();// 获取SessionHttpSession session = request.getSession();// 获取用户名String adminName = (String) session.getAttribute("adminName");// UUIDString uuidName = UUID.randomUUID().toString().replace("-","");LogMessage log = new LogMessage();log.setLogId(uuidName);log.setLogUser(adminName);log.setLogTime(new Date());String totalArg = null;//获取参数Object[] args = pjp.getArgs();for (Object arg : args) {totalArg += arg;totalArg += " ";}log.setLogMessage(totalArg);// 返回目标方法的签名MethodSignature methodSignature = (MethodSignature) pjp.getSignature();// 获取方法对象Method method = methodSignature.getMethod();// 获取方法名称String methodName = method.getName();String firstCode = methodName.substring(0,1);if (firstCode.equals("a")) {log.setLogAction("添加");} else if (firstCode.equals("m")) {log.setLogAction("修改");} else {log.setLogAction("删除");}
//        获取方法所在的类 及类型
//        System.out.println(methodSignature.getDeclaringType());// 获取方法所在的类String oldName = methodSignature.getDeclaringTypeName();String suffix = oldName.substring(oldName.lastIndexOf(".") + 1);log.setLogResource(suffix);
//        // 获取方法的注解
//        Annotation[] annotation = method.getAnnotations();Object obj = null;try {obj = pjp.proceed();log.setLogResult("success");} catch (Exception e) {e.printStackTrace();log.setLogResult("error");}logDAO.inserLog(log);return obj;}
}

该类实现的是用户操作后台的日志记录,实现了用户在进行增删改时记录用户信息,操作的类以及方法参数。

这样一个简单的AspectJ就实现了。

AspactJ框架常用注解

@PonitCut  // 声明切入点的注解
@Before    // 声明前置通知注解
@After     // 声明后置通知注解(原始方法执行正常或者非正常执行都会进入)
@AfterReturing // 声明后置通知注解(原始方法必须正常执行)
@AfterThrowing // 声明异常通知
@Around // 环绕通知注解

简单使用AspectJ相关推荐

  1. 【字节码插桩】AOP 技术 ( “字节码插桩“ 技术简介 | AspectJ 插桩工具 | ASM 插桩工具 )

    文章目录 一." 字节码插桩 " 技术简介 二.AspectJ 插桩工具 三.ASM 插桩工具 一." 字节码插桩 " 技术简介 性能优化 , 插件化 , 热修 ...

  2. aspectj 获取方法入参_深入探索编译插桩技术(二、AspectJ)

    本文来自jsonchao的投稿,个人微信:bcce5360 现如今,编译插桩技术已经深入 Android 开发中的各个领域,而 AOP 技术正是一种高效实现插桩的模式,它的出现正好给处于黑暗中的我们带 ...

  3. Spring3系列12-Spring AOP AspectJ

    本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...

  4. 使用AspectJ开发AOP更加便捷,你不知道嘛

    前文 中,已经讲解了Spring传统的AOP开发,但在实际开发中,我们都是使用AspectJ进行AOP开发. AspectJ 简介 AspectJ 是一个基于Java语言的独立的AOP框架. 在没有A ...

  5. Spring AOP AspectJ

    本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...

  6. android studio aspectj 配置,Android studio配置AspectJ

    一.首先说说使用AspectJ的意义 使用切点插入,减少冗余代码,但是因为是编译时插入,所以运行时会有性能损耗,但是总的来说微乎其微 二.运行原理 这是我原来的代码 @BehaviorTrace(&q ...

  7. JavaSpring全面总结

    Spring Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的框架. Spring重要的模块 Spring Core: 核心类库,提供IOC服务: Spring Context:提供 ...

  8. Spring入门——AOP(面向切面,切什么面?)

    SpringAOP 了解SpringAOP之前,我们先要了解一些关于代理的概念 什么是代理 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是 ...

  9. ajdt插件安装后无效_使用Eclipse和AJDT开发面向方面的Java应用程序

    AspectJ Eclipse开发工具(AJDT)是一个开源Eclipse技术项目,它提供开发和运行AspectJ应用程序所需的工具. 我们认为,好的工具在实现面向方面的编程的全部好处方面,尤其是在帮 ...

最新文章

  1. linux命令 pushd和popd
  2. malloc的内存分配之 malloc(0)的内存分配情况
  3. 配置zabbix及安装agent
  4. 交易引擎的对账状态为空的处理方式
  5. Hive Union操作中的隐式转换
  6. flex与j2ee的结合(flex+Spring)
  7. Android 虚线切割线
  8. 微信支付 (APP端开发)
  9. 华为P9移动定制版刷为联通移动双4G版本
  10. 如何更改XP系统默认字体 讨论话题……
  11. 手机聊天页面 html5,HTML5仿手机微信聊天界面
  12. python生成树状图_python如何把数据变成树状图
  13. 小丁在研究数学问题时遇到一个定义:对于排好顺序的k个数:x1,x2,x3,…,xk,称为数列Ak:x1,x2,x3,xk,其中k为整数且k≥3.定义V(Ak)=|x1-x2|+|x2-x3|+…+|x
  14. **视频会议设备连接注意事项**
  15. 在excel中用VBA生成PPT图表
  16. dataframe日期按周、按月、按季度聚合
  17. 德国变态驱蚊水,喷一次驱蚊12小时!打开门窗都能安心睡觉,比花露水好用10086倍...
  18. 【Mac使用技巧】QuickTime Player 如何让声音和视频同步加速播放 from csdner +HurryChen+
  19. 【Spring Cloud 11】软件架构设计
  20. (十八)享元模式详解(都市异能版) - 转

热门文章

  1. MeeGo的中国救亡之路:Jolla与迪信通牵手合作
  2. 【无标题】关于机械版CAD零件序号乱了重新排序的问题
  3. 做好防雷检测的重要意义和作用
  4. 上海达内python培训
  5. 子盒子在父盒子中水平垂直居中
  6. Vue - router vs route
  7. 由十多位架构师打造的《面试突击核心讲》到底有多强?肝完金三银四稳了。
  8. 今天来聊一下,俞敏洪把
  9. 每日裂变获客17000人,转转如何用红包裂变引爆私域增长?
  10. PMP考试时间是什么时候?