spring使用AOP做实现操作记录的功能

spring使用AOP做实现操作记录的功能

首先定义一个注解:

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface logAnnotation {

	/** 要执行的操作类型比如:add操作 **/
	public String operationType() default "";
	
	/** 要执行的具体操作比如:添加用户 **/
	public String operationName() default "";

}

写一个AOP类:

// 该注解标示该类为切面类
@Aspect
// 注入依赖
@Component
public class OperationLogAspect {

private HttpServletRequest request = null;

@Resource
ILogService logService;

/**
 *
 * @Description: 方法调用后触发   记录结束时间
 * @author fei.lei
 * @date 2016年11月23日 下午5:10
 * @param joinPoint
 */
public  void after(JoinPoint joinPoint) {
    request = getHttpServletRequest();
    HttpSession session = request.getSession();
    String account  = (String) session.getAttribute("account");
   //获取被代理的对象
    String targetName = joinPoint.getTarget().getClass().getName();
    //获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
    String methodName = joinPoint.getSignature().getName();
    //获取传入目标方法的参数对象
    Object[] arguments = joinPoint.getArgs();
    Class targetClass = null;
    try {
        targetClass = Class.forName(targetName);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Method[] methods = targetClass.getMethods();
    String operationName = "";
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            Class[] clazzs = method.getParameterTypes();
            if (clazzs!=null&&clazzs.length == arguments.length&&method.getAnnotation(logAnnotation.class)!=null) {
                operationName = method.getAnnotation(logAnnotation.class).operationName();
                break;
            }
        }
    }

    Object obj =request.getParameter("fileName");
    Log log = new Log();
    log.setOperation(operationName);
    if(account != null && operationName != ""){

        log.setUserid(Integer.valueOf(account));
        Date nowDate = new Date();
        long dateGetTime = nowDate.getTime();
        Timestamp timestamp = new Timestamp(dateGetTime);
        log.setOperationtime(timestamp);
        logService.insertLog(log);
    }
}

/**
 * @Description: 获取request
 * @author fei.lei
 * @date 2016年11月23日 下午5:10
 * @param
 * @return HttpServletRequest
 */
public HttpServletRequest getHttpServletRequest(){
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    ServletRequestAttributes sra = (ServletRequestAttributes)ra;
    HttpServletRequest request = sra.getRequest();
    return request;
}

}

在spring.xml中进行配置

<bean id="logAspect" class="com.honger.annotation.OperationLogAspect"></bean>

<aop:config>
    <!--调用日志类-->
    <aop:aspect id="LogAspect" ref="logAspect">
        <!--配置在controller包下所有的类在调用之前都会被拦截-->
        <aop:pointcut id="log" expression="execution(* com.honger.controller.UserController.*(..))"/>
        <!-- 方法后触发 -->
        <aop:after pointcut-ref="log" method="after"/>
    </aop:aspect>
</aop:config>

使用

最后在想要记录的操作上加上相应的注解就可以了 /** * 修改用户 * @param user 修改后的用户信息 * @return */ 

@RequestMapping("/update")
@ResponseBody
 @logAnnotation(operationType="修改操作",operationName="修改用户") 
public ServerResult update(User user) {
    //修改用户
    ServerResult<Integer> serverResult = userService.updateUser(user);
    UserRole userRole = new UserRole();
    userRole.setUid(user.getUid());
    userRole.setRid(Integer.parseInt(user.getRole()));
    //根据uid修改用户对应的rid
    userRoleService.update(userRole);
    return serverResult;
}

作者:haveFuner
来源:CSDN
原文:https://blog.csdn.net/qq_31118837/article/details/85986494
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值