首先需要了解一下Spring实现AOP的4种方法,参考下面这篇博文:
Spring实现AOP的4种方式
https://blog.csdn.net/udbnny/article/details/5870076
本文将采用第2种方法,即@AspectJ注解驱动的切面
当然还有AOP中的5种通知方式也务必了解,参考如下博文:
https://blog.csdn.net/sinat_28978689/article/details/62215513
spring知识六——AOP五大通知
本文采用的是@Around,即环绕通知,虽然是杀鸡用牛刀,但杀的痛快又有何妨。
明白了以上知识点,就可以双手搭上键盘飞舞起来了,直接上代码:
1.创建一个注解类,取名为ArchivesLog
package com.dcms.admin.annotation;
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ArchivesLog {
String message() default "";
}
2.新建类ArchivesLogAspect
package com.dcms.admin.annotation;
import com.dcms.admin.bean.Log;
import com.dcms.admin.bean.User;
import com.dcms.admin.service.ILogService;
import com.dcms.util.IPUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
@Aspect
@Component
public class ArchivesLogAspect {
@Autowired
private HttpServletRequest request;
@Autowired
private HttpSession session;
@Autowired
private ILogService logService;
private static Log log = new Log();
@Around("within(com.dcms..*) && @annotation(archivesLog)")
public Object around(ProceedingJoinPoint pjd, ArchivesLog archivesLog) throws Throwable {
log.setLogId(null);
log.setCtime(new Date());
long startTime=System.currentTimeMillis();
//类名
String className = pjd.g