1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.编写注解类
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;
/**
* 注解本身是没有功能的,就和xml一样.
* 注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置.
* @author Administrator
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AdminLog {
/*
type 操作类型:新增,修改,删除,其他
module 操作的功能模块
*/
String type();
String module();
}
3.编写切面,切点,各种通知
import xxx.service.AdminLogService;
import java.lang.reflect.Method;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect //声明一个切面
@Component
@AllArgsConstructor
public class LogAspect {
AdminLogService adminLogService;
//声明一个切点
@Pointcut("@annotation(xxx.aop.AdminLog)")
public void annotationPoinCut() {
}
/**
* 基于注解的拦截
*/
//声明一个建言
@After("annotationPoinCut()")
public void after(JoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getResponse();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
//方法入参
Object[] args = joinPoint.getArgs();
//获取目标的类
Object target = joinPoint.getTarget();
//获取方法上的注解的实例,就可以拿到里面的信息
//写入数据库
AdminLog action = method.getAnnotation(AdminLog.class);
xxx.entity.AdminLog adminLog
= new xxx.entity.AdminLog();
adminLog.setName("default");
adminLog.setType(action.type());
adminLog.setModule(action.module());
adminLog.setDetail("detail");
adminLog.setCreation(new Date());
try {
adminLogService.insert(adminLog);
} catch (Exception e) {
e.printStackTrace();
}
//通过反射可以获得注解上的属性,然后做日志记录相关的操作,下面的相同.
System.out.println("注解式拦截: " + action.type());
}
@AfterReturning(value = "annotationPoinCut()",returning = "methodResult")
public void afterReturning(JoinPoint joinPoint, Object methodResult) {
MethodSignature ms = (MethodSignature) joinPoint.getSignature();
Method method = ms.getMethod();
System.out.println("请求方法为:" + method.getName());
System.out.println("请求返回内容为:" + methodResult.toString());
}
/**
* 基于方法的拦截
*/
@Before("annotationPoinCut()")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则拦截: " + method.getName());
}
// /**
// * 基于方法的拦截
// * @param joinPoint
// */
// @Before("execution(* com.hgh.springboot_1.test1_3_2.DemoMethodServices.*(..))")
// public void before(JoinPoint joinPoint){
// MethodSignature signature = (MethodSignature) joinPoint.getSignature();
// Method method = signature.getMethod();
// System.out.println("方法规则拦截: " + method.getName());
// }
}
4.测试方法
/**
* http://127.0.0.1:8080/game/aopTest
*/
TestService testService;
@RequestMapping("/aopTest")
public Result aopTest() {
String s = testService.aopTest("value1","value2");
return RespResult.success(s);
}
service
import xxx.aop.AdminLog;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
@AllArgsConstructor
public class TestService {
@AdminLog(type = "新增",module = "测试模块")
public String aopTest(String v1,String v2) {
return "success";
}
}