Spring AOP 切面实现操作日志
- 创建接口注解日志类
- package com.fh.service.logAop;
- /**
- * Created by caozengling on 2018/7/21.
- */
- import java.lang.annotation.*;
- /**
- * 日志切面注解
- */
- @Target({ ElementType.METHOD, ElementType.TYPE })
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface MethodLog {
- String remark() default "";
- String operType() default "0";
- // String desc() default "";
- }
- 切面实现
- package com.fh.service.logAop;
- /**
- * Created by caozengling on 2018/7/21.
- */
- import com.fh.dao.DaoSupport;
- 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.stereotype.Component;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import java.awt.geom.Area;
- import java.lang.reflect.Method;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- /**
- * 日志切面实现
- */
- @Component
- @Aspect
- public class LogService {
- @Resource(name = "daoSupport")
- private DaoSupport dao;
- public LogService() {
- System.out.println("Aop");
- }
- /**
- * 切点
- */
- @Pointcut("@annotation(com.fh.service.logAop.MethodLog)")
- public void methodCachePointcut() { }
- /**
- * 切面
- *
- * @param point
- * @return
- * @throws Throwable
- */
- @Around("methodCachePointcut()")
- public Object around(ProceedingJoinPoint point) throws Throwable {
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
- .getRequestAttributes()).getRequest();
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
- Calendar ca = Calendar.getInstance();
- String operDate = df.format(ca.getTime());
- String loginName;
- String name;
- String methodRemark = getMthodRemark(point);
- String methodName = point.getSignature().getName();
- String packages = point.getThis().getClass().getName();
- if (packages.indexOf("$$EnhancerByCGLIB$$") > -1) { // 如果是CGLIB动态生成的类
- try {
- packages = packages.substring(0, packages.indexOf("$$"));
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- String operatingcontent = "";
- Object[] method_param = null;
- Object object;
- try {
- method_param = point.getArgs(); //获取方法参数
- // String param=(String) point.proceed(point.getArgs());
- object = point.proceed();
- } catch (Exception e) {
- // 异常处理记录日志..log.error(e);
- throw e;
- }
- Area area = (Area) method_param[0];
- // System.out.println("日志实体:"+sysLog.getLoginName()+sysLog.getMethodRemark()+sysLog.getOperatingcontent());
- return object;
- }
- /**
- * 方法异常时调用
- *
- * @param ex
- */
- public void afterThrowing(Exception ex) {
- System.out.println("afterThrowing");
- System.out.println(ex);
- }
- /**
- * 获取方法中的中文备注
- *
- * @param joinPoint
- * @return
- * @throws Exception
- */
- public static String getMthodRemark(ProceedingJoinPoint joinPoint) throws Exception {
- String targetName = joinPoint.getTarget().getClass().getName();
- String methodName = joinPoint.getSignature().getName();
- Object[] arguments = joinPoint.getArgs();
- Class targetClass = Class.forName(targetName);
- Method[] method = targetClass.getMethods();
- String methode = "";
- for (Method m : method) {
- if (m.getName().equals(methodName)) {
- Class[] tmpCs = m.getParameterTypes();
- if (tmpCs.length == arguments.length) {
- MethodLog methodCache = m.getAnnotation(MethodLog.class);
- if (methodCache != null) {
- methode = methodCache.remark();
- }
- break;
- }
- }
- }
- return methode;
- }
- }
- 方法切入,这里只是举个例子,具体逻辑切入点请自行添加。
- 依赖:
- springboot:
- <!--spring切面aop依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId>
- </dependency>
- 在application.properties文件里加这样一条配置
- spring.aop.auto=true
- spring mvp :
在ApplicationContext-mvc.xml 中添加以下配置:
- <aop:aspectj-autoproxy proxy-target-class="true"/>
Spring AOP 切面实现操作日志的更多相关文章
- springboot—spring aop 实现系统操作日志记录存储到数据库
原文:https://www.jianshu.com/p/d0bbdf1974bd 采用方案: 使用spring 的 aop 技术切到自定义注解上,针对不同注解标志进行参数解析,记录日志 缺点是要针对 ...
- Spring Boot 2.X(八):Spring AOP 实现简单的日志切面
AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- 利用Spring AOP切面对用户访问进行监控
开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...
- Spring AOP切面的时候参数的传递
Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...
- spring AOP(切面) 表达式介绍
在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...
- 使用Spring AOP切面解决数据库读写分离
http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...
- 【spring】aop切面通知,日志处理
1.spring的切面编程 概念原理可以看这里:http://blog.csdn.net/moreevan/article/details/11977115 2.所需要的jar包 maven引入jar ...
- Spring AOP 实现写事件日志功能
什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...
随机推荐
- node 把前台传来的base64码转成图片存放
最近做个人网站头像修改用到了,在做头像修改,先做了一个图片切割,只需要上传你选中部分, 如图 这种需求 应该还是会遇到的, http://pan.baidu.com/s/1boVkn1t 这是裁剪图片 ...
- myeclipse 代码提示
from http://fuyiyuan2011.iteye.com/blog/1258264 在软件开发过程中,有了代码提示能使开发能够更加快捷与便利.但在Eclipse ,MyEclipse等ja ...
- VULKAN学习笔记-inter教学四篇
--交换链相关函数:实例层vkCreateWin32SurfaceKHRvkDestroySurfaceKHRvkGetPhysicalDeviceSurfaceSurportKHRvkGetPhys ...
- conflicting types for ‘方法名’ 的错误
将main()的实现写在drawShapes(),drawCircle(),drawRectangle()...之前. 结果编译的时候出现了 conflicting types for " ...
- No matter how hard it is or no matter how bad it gets, I am going to make it!
No matter how hard it is or no matter how bad it gets, I am going to make it! He always had a yearni ...
- 【336】Tutorial of Endnote
Now I start to use Endnote to manage my literatures and I need to learn how to use it. Below is some ...
- 如何打开Windows Server 2008 R2的域安全策略
今天安装了Windows Server 2008 R2系统,并且建了域环境,在添加新用户的时候,发现用简单的密码时域安全策略提示密码复杂度不够,于是我就想在域安全策略里面把密码复杂度降低一点,但是很快 ...
- Centos7 vnc
这是一个关于怎样在你的 CentOS 7 上安装配置 VNC 服务的教程.当然这个教程也适合 RHEL 7 .在这个教程里,我们将学习什么是 VNC 以及怎样在 CentOS 7 上安装配置 VNC ...
- dubbo通信协议
对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况,本文参考dubbo官方文档 http://dubbo.io/User+Guide-zh.htm dubbo共支持如下几种通信协议: ...
- FastDFSClient工具类 文件上传下载
package cn.itcast.fastdfs.cliennt; import org.csource.common.NameValuePair; import org.csource.fastd ...