基于Spring的AOP实现自定义annotation操作日志
来源:互联网 发布:云计算和分布式计算 编辑:程序博客网 时间:2024/05/13 08:02
引入项目需要的AOP包
<!--aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.4</version> </dependency>
要在springmvc的配置文件里加入配置文件,并指明切点类的的包,好被自动扫描
<aop:aspectj-autoproxy/> <context:component-scan base-package="com..util"/> <aop:aspectj-autoproxy proxy-target-class="true" />
首先编写一个annotation的注解类
import java.lang.annotation.*;@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface SystemControllerLog { String description() default "";}
其次在编写用户管理的切点类,记录用户操作的的时间,ip,操作
import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;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 javax.servlet.http.HttpSession;import java.lang.reflect.Method;import java.util.Date;@Aspect@Componentpublic class SystemLogAspect { @Resource private EwslogDao ewslogDao; //Controller层切点,也就是上面那个注解的路径 @Pointcut("@annotation(com.util.SystemControllerLog)") public void controllerAspect() { } /** * 前置通知 用于拦截Controller层记录用户的操作 * * @param joinPoint 切点 */ @Before("controllerAspect()") public void doBefore(JoinPoint joinPoint) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession session = request.getSession(); //读取session中的用户 User user = (User) session.getAttribute("user"); String name =""; if(user!=null){ name = user.getUsername(); }else name = "匿名用户"; //请求的IP String ip = request.getRemoteAddr(); String description=""; try { description = getControllerMethodDescription(joinPoint); } catch (Exception e) { e.printStackTrace(); } Ewslog ewslog = new Ewslog(); ewslog.setLoginName(name); ewslog.setLogDate(new Date()); ewslog.setDescription(description); ewslog.setIpAddr(ip); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ip"+name); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ip"+description); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ip"+ip); ewslogDao.save(ewslog); } public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception { String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); String description = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { description = method.getAnnotation(SystemControllerLog. class).description(); break; } } } return description; }}
在需要记录的切面类上只需要添加一个注解就可以完成aop了。spring真是太强大了。
@RequestMapping("/loginOut") @SystemControllerLog(description = "用户登出") public String index(HttpServletRequest request){ HttpSession session = request.getSession(); session.setAttribute("user", null); return "../index"; }
0 0
- 基于Spring的AOP实现自定义annotation操作日志
- 基于Spring的AOP实现自定义annotation操作日志
- @interface 自定义annotation 通过AOP来实现人员操作日志
- @interface 自定义annotation 通过AOP来实现人员操作日志
- 利用Java Annotation 和 Spring AOP实现在Controller层面的操作日志记录
- spring AOP annotation 实现日志服务
- spring的AOP实现记录操作日志
- Spring的AOP实现日志管理操作
- Spring AOP的annotation实现
- Spring aop +自定义annotation
- 基于Annotation的Spring AOP: @Before
- 基于Annotation的Spring AOP: @AfterReturning
- 基于Annotation的Spring AOP: @AfterThrowing
- 基于Annotation的Spring AOP: @After
- 基于Annotation的Spring AOP: @Around
- Spring基于annotation的AOP(AspectJ)
- Spring AOP 的引入(三 基于annotation)
- Spring AOP功能--基于Annotation的方式
- jzoj 4243. 【五校联考6day1】c 分块
- mybatis 3.4.2 DataSourceFactory与DataSource
- Codeforces-546D Soldier and Number Game 【质因子分解+打表+DP】
- 什么是JSON
- Oulipo_poj3461_kmp
- 基于Spring的AOP实现自定义annotation操作日志
- C++泛型编程2——类模板,容器适配器,仿函数
- 为何计算机人应该懂一点编译知识?
- Handler线程学习心得
- android异常捕获,上线前的操作
- Hibernate 的配置
- 归并排序
- C++:Boost库智能指针_shared_array
- 矩阵形式回归系数W推导