SpringBoot提供了强大AOP支持,我们前面讲解过AOP面向切面,所以这里具体AOP原理就补具体介绍;
AOP切面主要是切方法,我们一般搞一些日志分析和事务操作,要用到切面,类似拦截器;
@Aspect注解是切面注解类
@Pointcut切点定义
@Before是方法执行前调用
@After是方法执行后调用
@AfterReturning方法执行返回值调用
Service层本身就可以切入事务,所以我们这类搞个常用的 切controller层方法
每个执行controller层的方法 都记录下请求Url,访问者IP 执行类方法参数等信息;
定义一个切面类:RequestAspect
import
javax.servlet.http.HttpServletRequest;
import
org.apache.log4j.Logger;
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.springframework.stereotype.Component;
import
org.springframework.web.context.request.RequestContextHolder;
import
org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Component
public
class
RequestAspect {
private
Logger logger=Logger.getLogger(RequestAspect.
class
);
@Pointcut
(
"execution(public * com.java1234.controller.*.*(..))"
)
public
void
log(){
}
@Before
(
"log()"
)
public
void
deoBefore(JoinPoint joinPoint){
logger.info(
"方法执行前..."
);
ServletRequestAttributes sra=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=sra.getRequest();
logger.info(
"url:"
+request.getRequestURI());
logger.info(
"ip:"
+request.getRemoteHost());
logger.info(
"method:"
+request.getMethod());
logger.info(
"class_method:"
+joinPoint.getSignature().getDeclaringTypeName()+
"."
+joinPoint.getSignature().getName());
logger.info(
"args:"
+joinPoint.getArgs());
}
@After
(
"log()"
)
public
void
doAfter(JoinPoint joinPoint){
logger.info(
"方法执行后..."
);
}
@AfterReturning
(returning=
"result"
,pointcut=
"log()"
)
public
void
doAfterReturning(Object result){
logger.info(
"执行返回值:"
+result);
}
}
execution(public * com.java1234.controller.*.*(..)) 这个定义 意思是 对 com.java1234.controller包下的任意类,任意方法,任意参数,任意返回值的方法都进行切入
我们测试 StudentController
请求:http://localhost:8888/studentAdd.html
点击“提交”,
控制台显示:
2017-08-11 11:22:46.357 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : 方法执行前...
2017-08-11 11:22:46.358 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : url:/student/add
2017-08-11 11:22:46.358 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : ip:0:0:0:0:0:0:0:1
2017-08-11 11:22:46.358 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : method:POST
2017-08-11 11:22:46.358 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : class_method:com.java1234.controller.StudentController.add
2017-08-11 11:22:46.358 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : args:[Ljava.lang.Object;@fd1ee9
Hibernate: insert into t_student (age, name) values (?, ?)
2017-08-11 11:22:46.372 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : 方法执行后...
2017-08-11 11:22:46.373 INFO 9896 --- [nio-8888-exec-9] com.java1234.aspect.RequestAspect : 执行返回值:添加成功!
这里得到了我们需要的信息;
当然这里用到了日志 springboot推荐logback log4j的升级版 用法基本差不多;