SpringMVC利用AOP實現自定義註解記錄日誌

SpringMVC利用AOP實現自定義註解記錄日誌

1、interface Log


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

/**
 * @author: football98
 * @createTime: 16-6-23
 * @classDescription: 日誌annotation類。
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Log {
    String name() default "";
}

2、LogAop


import java.lang.reflect.Method;
import java.util.Date;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import zteict.qinhuangdao.framework.base.annotation.Log;
import zteict.qinhuangdao.framework.base.po.Tlog;
import zteict.qinhuangdao.framework.base.service.BaseService;
import zteict.qinhuangdao.framework.common.security.vo.LoginUserVO;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: football98
 * @createTime: 16-6-23
 * @classDescription: 日誌aop類。
 */
@Aspect
@Component
public class LogAop {

    @Autowired
    private BaseService baseService;

    private ThreadLocal<Long> startTime = new ThreadLocal<Long>();

    private String loginName;

    @Before("@annotation(zteict.qinhuangdao.framework.base.annotation.Log)")
    public void beforeExec(JoinPoint joinPoint){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //獲取操作用戶
        LoginUserVO vo = (LoginUserVO)request.getSession().getAttribute("LOGINUSER");
        startTime.set(System.currentTimeMillis());
        if(vo != null&&!vo.getLoginname().equals("")){
            loginName = vo.getLoginname();
        }
    }

    @After("@annotation(zteict.qinhuangdao.framework.base.annotation.Log)")
    public void afterExec(JoinPoint joinPoint){
        long endtime = System.currentTimeMillis();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //獲取操作方法
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        Method method = ms.getMethod();
        //保存日誌對象
        Tlog log = new Tlog();
        log.setTlogmethod(method.getName());
        log.setTlogname(method.getAnnotation(Log.class).name());
        if(loginName!=null&&!loginName.equals("")){
            log.setTloguser(loginName);
            log.setTlogip(request.getRemoteAddr());
            log.setTlogdate(new Date(endtime));
            log.setTlogtime((endtime - startTime.get())+"ms");
            baseService.save(log);
        }else{
            LoginUserVO vo = (LoginUserVO)request.getSession().getAttribute("LOGINUSER");
            if(vo != null&&!vo.getLoginname().equals("")){
                log.setTloguser(vo.getLoginname());
                log.setTlogip(request.getRemoteAddr());
                log.setTlogdate(new Date(endtime));
                log.setTlogtime((endtime - startTime.get())+"ms");
                baseService.save(log);
            }
        }
    }
}
3、springmvn如果要使用aop註解,必須進行如下配置

	<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>



發佈了34 篇原創文章 · 獲贊 6 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章