spring boot中使用AOP
2017-07-25 21:17
316 查看
spring boot 中使用AOP,需要先在pom.xml中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
然后编写切面程序,例如:
@Aspect
@Component
public class HttpAspect {
private static final Logger logger=LoggerFactory.getLogger(HttpAspect.class);
//配置切点
@Pointcut("execution(public * com.springboot.controller.GirlController.*(..))")
public void log(){
}
//joinPoint用于获取域切入点方法有关的信息
@Before("log()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=requestAttributes.getRequest();
//url
logger.info("url={}",request.getRequestURL());
//method
logger.info("method={}",request.getMethod());
//ip
logger.info("ip={}",request.getLocalAddr());
//类方法
logger.info("class_method={}",joinPoint.getSignature().getDeclaringType()+"."+joinPoint.getSignature().getName());
//方法参数
logger.info("args={}",joinPoint.getArgs());
}
@After("log()")
public void doAfter(){
logger.info("doAfter={}",2222222);
}
//得到response返回的数据,returning代表切入点方法返回的数据
@AfterReturning(returning = "object",pointcut = "log()")
public void doAfterReturning(Object object){
logger.info("response={}",object.toString());
}
}
需要在类上添加@Aspect注解表明这是一个切面,添加@Component注解是为了能够让spring的bean容器对齐进行管理,然后在对应的方法上添加需要表明什么时候执行切面的对应注解,如果需要获取切入点有关方法的信息,可以传入JoinPoint对象。要得到response响应的数据,参考上面@AfterReturning注解的使用。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
然后编写切面程序,例如:
@Aspect
@Component
public class HttpAspect {
private static final Logger logger=LoggerFactory.getLogger(HttpAspect.class);
//配置切点
@Pointcut("execution(public * com.springboot.controller.GirlController.*(..))")
public void log(){
}
//joinPoint用于获取域切入点方法有关的信息
@Before("log()")
public void doBefore(JoinPoint joinPoint){
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request=requestAttributes.getRequest();
//url
logger.info("url={}",request.getRequestURL());
//method
logger.info("method={}",request.getMethod());
//ip
logger.info("ip={}",request.getLocalAddr());
//类方法
logger.info("class_method={}",joinPoint.getSignature().getDeclaringType()+"."+joinPoint.getSignature().getName());
//方法参数
logger.info("args={}",joinPoint.getArgs());
}
@After("log()")
public void doAfter(){
logger.info("doAfter={}",2222222);
}
//得到response返回的数据,returning代表切入点方法返回的数据
@AfterReturning(returning = "object",pointcut = "log()")
public void doAfterReturning(Object object){
logger.info("response={}",object.toString());
}
}
需要在类上添加@Aspect注解表明这是一个切面,添加@Component注解是为了能够让spring的bean容器对齐进行管理,然后在对应的方法上添加需要表明什么时候执行切面的对应注解,如果需要获取切入点有关方法的信息,可以传入JoinPoint对象。要得到response响应的数据,参考上面@AfterReturning注解的使用。
相关文章推荐
- 【HAVENT原创】使用 Spring Boot 的 AOP 自定义注解
- 【SpringBoot】Spring Boot进阶之Web进阶( 第2章 Web进阶-使用AOP处理请求 )
- SpringBoot中使用AOP打印接口日志的方法
- Spring boot中使用aop详解
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十)使用AOP统一处理Web请求日志(1)
- SpringBoot项目中使用AOP
- Spring Boot中使用AOP统一处理Web请求日志
- Spring Boot 使用AOP统一处理Web请求日志
- SpringBoot 使用AOP处理请求
- springboot 中使用 AOP 打印 类名、方法名、方法参数名称和方法参数值
- SpringBoot中使用AOP 监控sql耗时
- SpringBoot基础教程2-1-6 日志规范-使用AOP统一处理Web日志
- 使用Spring Boot的AOP处理自定义注解
- SpringBoot开发详解(六)-- 异常统一管理以及AOP的使用
- Spring Boot中使用AOP统一处理Web请求日志
- SpringBoot项目中使用AOP的方法
- springboot 中使用AOP
- (十四)SpringBoot使用AOP统一处理Web请求日志添加MDC
- Spring Boot中使用AOP统一处理web层异常的方法
- spring boot aop的使用