基于Spring的AOP实现自定义annotation操作日志
2016-08-13 21:54
615 查看
引入项目需要的AOP包
要在springmvc的配置文件里加入配置文件,并指明切点类的的包,好被自动扫描
首先编写一个annotation的注解类
其次在编写用户管理的切点类,记录用户操作的的时间,ip,操作
在需要记录的切面类上只需要添加一个注解就可以完成aop了。spring真是太强大了。
<!--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) @Documented public @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 @Component public 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"; }
相关文章推荐
- 基于SSM利用SpringAOP切面及自定义注解 记录每次操作记录(操作日志 同理)
- @interface 自定义annotation 通过AOP来实现人员操作日志
- @interface 自定义annotation 通过AOP来实现人员操作日志
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- spring AOP自定义注解方式实现日志管理
- spring2.0基于Annotation的AOP实现以及AOP事务
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- spring+mybatis基于 AOP实现业务日志管理
- spring 基于aop 实现日志输出 定义在controller层无效
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring AOP 自定义注解记录操作日志
- springAOP自定义注解方式实现日志管理
- Spring Aop自定义注解拦截Controller实现日志管理
- spring的AOP实现记录操作日志
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- [EntLib]微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录
- spring aop 实现用户操作日志记录功能