在SpringBoot应用中实现一个AOP Demo,主要功能就是在控制器方法执行前和执行后记录一些日志。
1、application.yml
logging:
level:
root: info
file:
#path参数对name不起作用,默认文件名为spring.log
path: d:\\aopLogs
#name: d:\\aopLogs\mySpring.log
2、注解文件
package com.space.aspect.anno;
import java.lang.annotation.*;
/**
* @package com.space.aspect.anno
* @file SysUserInfo .java
* @description
* @author spring
* @time 2020/5/19 17:04
* @version 1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysUserInfo {
String myName() default "spring";
String myAge() default "18";
}
3、切面定义文件
package com.space.aspect.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* @author spring
* @version 1.0
* @package com.space.aspect.aspect
* @file SysUserInfoAspect.java
* @description
* @time 2020/5/19 17:06
*/
@Aspect
@Component
public class SysUserInfoAspect {
private static Logger logger=LoggerFactory.getLogger(SysUserInfoAspect.class);
@Pointcut("@annotation(com.space.aspect.anno.SysUserInfo)")
public void userPointCut() {
}
@Around("userPointCut()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
System.out.println("enter SysUserInfoAspect/doAround......");
logger.info("enter SysUserInfoAspect/doAround......");
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).
getRequest();
String requestUrl = request.getRequestURL().toString();
System.out.println("requestUrl is " + requestUrl);
logger.info("requestUrl is "+requestUrl);
//修改request参数;
MethodSignature signature = (MethodSignature) point.getSignature();
String methodName = signature.getName();
String[] allParams=signature.getParameterNames();
System.out.println("methodName="+methodName);
logger.info("methodName is "+methodName);
for(String oneParameterName:allParams){
System.out.println(oneParameterName);
logger.info(oneParameterName);
}
Method method = signature.getMethod();
SysUserInfo sysUserInfo= method.getAnnotation(SysUserInfo.class);
logger.info(sysUserInfo.myName());
logger.info(sysUserInfo.myAge());
Object[] args = point.getArgs();
for(int i=0;i<args.length;i++){
System.out.println(args[i]);
logger.info(args[i].toString());
args[i]=args[i].toString().concat("+111");
}
Object result=point.proceed(args);
System.out.println("end end end......");
logger.info("end end end......");
return result;
}
}
4、定义控制器方法
package com.space.aspect.controller;
import com.space.aspect.anno.SysUserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author spring
* @version 1.0
* @package com.space.aspect.controller
* @file TestUserController.java
* @description
* @time 2020/5/19 17:29
*/
@RestController
public class TestUserController {
@SysUserInfo(myName="pig",myAge = "100")
@GetMapping("/showOneUserInfo")
public Object showOneUserInfo(@RequestParam("myName") String name1,
@RequestParam("myAge") String age1){
Map<String,Object> user=new HashMap< >(2);
user.put("name",name1);
user.put("age",age1);
return user;
}
}
5、运行
浏览器URL
http://localhost:8080/showOneUserInfo?myName=spring&&myAge=10
浏览器运行结果为------
{“name”:“spring+111”,“age”:“10+111”}
日志文件D:\aopLogs\spring.log中AOP产生的内容为------
2020-06-14 17:13:45.470 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : enter SysUserInfoAspect/doAround…
2020-06-14 17:13:45.471 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : requestUrl is http://localhost:8080/showOneUserInfo
2020-06-14 17:13:45.472 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : methodName is showOneUserInfo
2020-06-14 17:13:45.473 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : name1
2020-06-14 17:13:45.473 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : age1
2020-06-14 17:13:45.474 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : 二师兄
2020-06-14 17:13:45.475 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : 1
2020-06-14 17:13:45.476 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : spring
2020-06-14 17:13:45.477 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : 10
2020-06-14 17:13:45.477 INFO 7432 — [http-nio-8080-exec-4] c.space.aspect.aspect.SysUserInfoAspect : end end end…