Spring-Aop之xml配置实现日志管理

最近项目要做一个日志功能,Spring Aop注解方式我已经实现了,以下是我用xml配置方式来实现。

创建日志注解

package com.wyj.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 日志注解
 * 
 * 
 * @author:WangYuanJun
 * @date:2016年8月27日 下午8:39:42
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

    String action() default "";//动作

}

创建切面通知类

package com.wyj.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wyj.annotation.SysLog;
import com.wyj.entity.SysLogEntity;
import com.wyj.service.SysLogService;

/**
 * 日志切面通知
 * 
 * 
 * @author:WangYuanJun
 * @date:2016年8月26日 下午8:39:42
 */
public class SysLogAspect {

    @Autowired
    private SysLogService sysLogService;

    /**
     * 环绕通知
     * 
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    public Object aroud(ProceedingJoinPoint joinPoint) throws Throwable {

        // 开始时间
        long beginTime = System.currentTimeMillis();

        // 执行目标方法
        Object result = joinPoint.proceed();

        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        // 保存日志
        saveSysLog(joinPoint, time);
        return result;
    }

    /**
     * 保存日志
     * 
     * @param joinPoint
     * @param time
     */
    private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLogEntity sysLogEntity = new SysLogEntity();
        SysLog sysLog = method.getAnnotation(SysLog.class);
        if (sysLog != null) {

            // 注解上的描述
            sysLogEntity.setOperation(sysLog.action());
        }

        // 获取目标类名
        String className = joinPoint.getTarget().getClass().getName();

        // 获取方法名
        String methodName = signature.getName();
        sysLogEntity.setMethod(className + "." + methodName + "()");

        // 请求的参数
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length != 0 && args[0] != null) {
            sysLogEntity.setParams(args[0].toString());
        }
        sysLogEntity.setTime(time);

        // 保存系统日志
        sysLogService.save(sysLogEntity);
    }
}

AspectJ配置文件

<!-- 切面 -->
<bean id="sysLogAspect" class="com.wyj.aspect.SysLogAspect"></bean>

<aop:config>
    <aop:aspect ref="sysLogAspect">
        <aop:pointcut expression="@annotation(com.wyj.annotation.SysLog)" id="sysLogPointcut"/>
        <aop:around method="aroud" pointcut-ref="sysLogPointcut"/>
    </aop:aspect>
</aop:config>

日志注解的应用

这里写图片描述

效果

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值