基于注解的aop实现用户操作日志管理
来源:互联网 发布:超图软件最新消息 编辑:程序博客网 时间:2024/06/08 10:32
AOP是spring的核心之一。用AOP来实现用户操作日志管理非常合适。
学东西应该先具体再抽象。下面直接上代码
- pom.xml
<!-- 面向切面的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency>
- SpringServlet.xml
<!-- aop 切面 --> <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> <context:component-scan base-package="com.kylin.aep.aop"></context:component-scan> <!-- 强制使用cglib代理,如果不设置,将默认使用jdk的代理,但是jdk的代理是基于接口的 --> <aop:aspectj-autoproxy proxy-target-class="true" />
- ControllerAspect.java
@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).*/public class ControllerAspect { @Autowired @Qualifier("SystemManageServiceImpl") SystemManageService systemManageServiceImpl; private static final Logger log = Logger.getLogger(ControllerAspect.class); // 方法执行之前被调用执行 /* 定义切入点 (配置在com.mycomp.myproj.controller包下所有后缀为Controller的类的所有方法在调用之前都会被拦截) 用户的所有操作,最终都是页面请求controller中的方法。 controller中方法执行之前会执行下面的方法,在这个时候写入日志。 */ @Before("execution(* com.mycomp.myproj.controller.*Controller.*(..))") public void permissionCheck(JoinPoint point) throws Throwable { // 获取调用的controller的类名 String className = point.getSignature().getDeclaringType().getSimpleName(); // 获取调用的controller类中的方法名 String methodName = point.getSignature().getName(); String operation = className + "." + methodName; //前端调用的方法和传入的参照都写入log log.info("控制器" + className + "." + methodName + "入参:" + Arrays.toString(point.getArgs())); // 获取方法中的参数 Object[] args = point.getArgs(); if (args.length > 0 && args[args.length - 1] instanceof HttpSession) { //参数列表的最后一个是httpsession HttpSession session = (HttpSession) args[args.length - 1]; //从session中获取当前登录用户 User user = (User) session.getAttribute("user"); if (user != null) { //操作日志写入日志表 //后面写入数据库之前会对operation做一个中文转义,毕竟是给用户看的。 systemManageServiceImpl.saveSystemLog(user, operation, 0); } else { log.info("未知用户进行" + operation + "操作!"); } } }}
这样,页面每一次请求都会自动记录一次日志。后面写业务代码的时候就不用关心用户操作日志了。
ps:当然,其他的errer和debug信息,该写log还是要写,方便后面查错。
阅读全文
0 0
- 基于注解的aop实现用户操作日志管理
- 基于aop的用户操作日志
- Spring的AOP实现日志管理操作
- SpringMVC自定义注解的方式AOP实现 日志管理
- 利用spring AOP注解实现日志管理
- aop注解方式实现全局日志管理
- spring自定义注解实现AOP日志管理
- aop注解方式实现日志管理
- aop+自定义注解实现操作日志记录
- aop+自定义注解实现操作日志记录
- 基于注解的AOP实现
- 基于Spring的AOP实现自定义annotation操作日志
- 基于Spring的AOP实现自定义annotation操作日志
- 基于AOP的日志实现!
- AOP实现用户操作日志记录
- 使用Spring AOP使用注解记录用户操作日志
- Spring自定义注解+Aop记录用户操作日志
- spring mvc +spring aop结合注解的 用户操作日志记录
- 第十四周项目一——(1)验证算法之哈希表
- 第十三周项目二C/C++二叉树排序树中查找的路径
- 改PCB的一些问题与方法
- iOS实用Demo
- linux ubuntu 与window 的共享文件夹 samba共享文件夹
- 基于注解的aop实现用户操作日志管理
- mybatis 整合spring之mapperLocations配置的问题
- 第十一周-项目4-5-应用图的深度优先遍历思路求解问题
- CentOS下安装JDK1.8.0_121
- php 判断输入日期 的本周,本月,本年的起止日期
- java如何读取文件到另一个文件
- 在js或者jquery里面能使用el表达式吗
- 技术分享连载(七十一)
- 数据结构上机实践第14周项目1(2)