- pom依赖:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
- 自定义接口注解类:
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
@Target({METHOD,TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Operation {
String name();
}
- 切面操作类:
import com.zbk.entity.LogBook;
import com.zbk.entity.User;
import com.zbk.tools.Operation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
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 javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
@Component
@Aspect
public class BookAOP {
@Autowired
private LogBookService logBookService;
@Pointcut("execution(* com.zbk.controller..*.*(..))")
public void method(){}
@Pointcut("execution(* com.zbk.controller..*.*(..))") //切点
public void webExceptionLog(){}
/**
* 正常日志信息
* @param joinPoint
*/
@AfterReturning("method()")
public void afterReturning(JoinPoint joinPoint){
insertLogBook(joinPoint,"正常");
}
/**
* 异常通知 用于拦截异常日志
* @param joinPoint
* @param e
*/
@AfterThrowing(pointcut = "webExceptionLog()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
insertLogBook(joinPoint,"错误");
}
private void insertLogBook(JoinPoint joinPoint,String logType){
LogBook logBook = new LogBook();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 2:通过springAOP切面JoinPoint类对象,获取该类,或者该方法,或者该方法的参数
Class<? extends Object> clazz = joinPoint.getTarget().getClass();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Subject subject = SecurityUtils.getSubject();
User user = (User) subject.getPrincipal();
Method method = signature.getMethod();
Method[] methods = clazz.getDeclaredMethods();
String methodOperation = "";
for (Method m : methods) {
if (m.equals(method)) {
methodOperation = m.getName();
if (m.isAnnotationPresent(Operation.class)) {
methodOperation = m.getAnnotation(Operation.class).name();
}
}
}
Date date = new Date();
logBook.setCreatTime(date);
logBook.setLogContent(methodOperation);
logBook.setUserOperation(user.getUserName());
logBook.setLogType(logType);
logBookService.insertLogBook(logBook);
}
}
- 使用
@Operation(name="查询字典列表")
@ResponseBody
@RequestMapping(value = "/getDataDictionaryList")
public Map<String,Object> getDataDictionaryList(@RequestParam Map<String, Object> params){
int totalCount = dataDictionaryService.dataCount(params);
Map<String, Object> rePutParams = LayuiUtil.rePutParams(params);
List<DataDictionary> dataDictionaryList = dataDictionaryService.getDataDictionaryList(rePutParams);
return LayuiUtil.dataList(totalCount, dataDictionaryList);
}
- 效果
有问题望指正!!!