springboot使用AOP
springboot使用AOP
概述
AOP是一种编程范式,与语言无关,是一 种程序设计思想 ;
面向切面 (AOP): Aspect Oriented Programming
面向对象 (OOP) :Object Oriented Programming
面向过程 (POP) :Procedure Oriented Programming
使用案例:
- pom
<!-- aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
5
1
<!-- aop -->
2
<dependency>
3
<groupId>org.springframework.boot</groupId>
4
<artifactId>spring-boot-starter-aop</artifactId>
5
</dependency>
2.创建aop类
import java.text.SimpleDateFormat; import java.util.Date; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; /**service层的aop增强 */ @Aspect//aop必须注解 @Configuration//注入spring public class ServiceAop { private final static Logger logger=LoggerFactory.getLogger(ServiceAop.class);//使用日志 //设置增强的方法---- //service层所有方法都调用 //Pointcut定义公用方法execution可以被其他注解使用 @Pointcut("execution(public * com.demo1.mall.service.*.*(..))") //具体到每一个方法 public void webLog(){} @Around("webLog()")//环绕,或者此处直接写"execution(public * com.demo1.mall.service.*.*(..))" public void ServiceAopAround(ProceedingJoinPoint pro) throws Throwable { //logger.info("pre handle");//日志 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date start=new Date(); //执行前时间 logger.info("开始时间:"+(sdf.format(start)));//日志 //调用方法 pro.proceed(); //执行后时间 Date stop=new Date(); logger.info("结束时间:"+(sdf.format(stop)));//日志 //总时间 long time=stop.getTime()-start.getTime(); logger.info("时间总计:"+time+"ms");//日志 } } |
/**service层的aop增强
*/
@Aspect
@Configuration
public class ServiceAop {
private final static Logger logger=LoggerFactory.getLogger(ServiceAop.class);
@Pointcut("execution(public * com.demo1.mall.service.*.*(..))") //service层所有方法都调用
public void webLog(){}
@Around("webLog()")
public Object ServiceAopAround(ProceedingJoinPoint pro) throws Throwable {
//调用方法
Object object=pro.proceed();//获取方法返回值
return object;
}
}
x
1
/**service层的aop增强
2
*/
3
4
5
public class ServiceAop {
6
private final static Logger logger=LoggerFactory.getLogger(ServiceAop.class);
7
("execution(public * com.demo1.mall.service.*.*(..))") //service层所有方法都调用
8
public void webLog(){}
9
("webLog()")
10
public Object ServiceAopAround(ProceedingJoinPoint pro) throws Throwable {
11
//调用方法
12
Object object=pro.proceed();//获取方法返回值
13
return object;
14
}
15
}
获取请求信息,显示日志
/**
* 记录日志信息
*/
@Before("log()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();//获取请求
HttpServletRequest request = attributes.getRequest();
//url
logger.info("url={}", request.getRequestURL());
//method
logger.info("method={}", request.getMethod());//获取请求get/post类型
//ip
logger.info("ip={}", request.getRemoteAddr());//获取请求ip
//类方法
logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());//获取方法全路径名
//参数
logger.info("args={}", joinPoint.getArgs());//获取方法参数
}
23
1
/**
2
* 记录日志信息
3
*/
4
"log()") (
5
public void doBefore(JoinPoint joinPoint) {
6
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();//获取请求
7
HttpServletRequest request = attributes.getRequest();
8
9
//url
10
logger.info("url={}", request.getRequestURL());
11
12
//method
13
logger.info("method={}", request.getMethod());//获取请求get/post类型
14
15
//ip
16
logger.info("ip={}", request.getRemoteAddr());//获取请求ip
17
18
//类方法
19
logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());//获取方法全路径名
20
21
//参数
22
logger.info("args={}", joinPoint.getArgs());//获取方法参数
23
}
url=http://12Z.0,0,1:8080/girls method=GET ip=127.0.0.1 class_method=com.imooc.controller.GirlController.girlList args={} |
url=http://12Z.0,0,1:8080/girls/20 method=GET ip=127.0.0.1 class_method=com.imooc.controller.GirlController.girlList args=20 |
获取返回信息,设置日志
@AfterReturning(returning = "object", pointcut = "log()")//获取返回的信息
public void doAfterReturning(Object object) {//返回值
logger.info("response={}", object.toString());
}
4
1
returning = "object", pointcut = "log()")//获取返回的信息 (
2
public void doAfterReturning(Object object) {//返回值
3
logger.info("response={}", object.toString());
4
}