aop是面向切面编程的意思,它可以需要先选择一些切入点,然后对这些切入点进行拦截,注入统一的代码逻辑,这也是解耦的一种方式,也是为了避免重复的代码,让开发人员把关注点放在业务上。

引用包

'org.springframework.boot:spring-boot-starter-aop'

添加切入点

/**
* 基于com.lind.basic.controller包下的方法进行拦截.
*/
@Aspect
@Component
public class AopPrintConstController {
private static final Logger logger = LoggerFactory.getLogger(AopPrintConstController.class); /**
* 横切点,哪些方法需要被横切.
*/
@Pointcut(value = "execution(public * com.lind.basic.controller..*.*(..))")
public void cutOffPoint() {
} /**
* @param joinPoint 具体的方法之前执行.
*/
@Before("cutOffPoint()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
logger.info("cutOffPoint.before...");
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String requestURI = request.getRequestURI();
String remoteAddr = request.getRemoteAddr(); //这个方法取客户端ip"不够好"
String requestMethod = request.getMethod();
String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
logger.info("请求url=" + requestURI + ",客户端ip=" + remoteAddr + ",请求方式=" + requestMethod + ",请求的类名=" + declaringTypeName + ",方法名=" + methodName + ",入参=" + args); } /**
* 解用于获取方法的返回值.
*
* @param obj 返回值
*/
@AfterReturning(returning = "obj", pointcut = "cutOffPoint()")
public void doBefore(Object obj) throws Throwable {
logger.info("RESPONSE : " + obj);
} }

测试

当我们访问controller下的接口下,在控制台中将输出方法执行前和执行后的结果

com.lind.basic.AopPrintConstController   : 请求url=/hello/1,客户端ip=0:0:0:0:0:0:0:1,请求方式=GET,请求的类名=...
com.lind.basic.controller.H com.lind.basic.AopPrintConstController : RESPONSE : <200 OK,com.lind.basic.entity.Token@249f92d9,{}>

感想

事实上,springboot真的是一个强大的脚手架,它帮助我们把很多依赖都结合了,像今天说的aop,事实上springboot帮我们把以下包都结合在一起了,让开发人员引用时更方便。

springboot-aop集成了

  • spring-aop,
  • aspectjrt,
  • spectjweaver

springboot中使用aop技术的更多相关文章

  1. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

  2. SpringBoot图文教程5—SpringBoot 中使用Aop

    有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例 ...

  3. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

  4. 编写SpringBoot 中的AOP

    编写SpringBoot 中的AOP 在程序开发的过程中会使用到AOP的思想,面向切面进行开发,比如登录的验证,记录日志等等-频繁需要操作的步骤,在遇到这种情况时就要使用Spring 的AOP了 Sp ...

  5. SpringBoot中使用LoadTimeWeaving技术实现AOP功能

    目录 1. 关于LoadTimeWeaving 1.1 LTW与不同的切面织入时机 1.2 JDK实现LTW的原理 1.3 如何在Spring中实现LTW 2. Springboot中使用LTW实现A ...

  6. Spring框架中的AOP技术----配置文件方式

    1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...

  7. Spring框架中的AOP技术----注解方式

    利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...

  8. SpringBoot中使用AOP打印接口日志的方法(转载)

    前言 AOP 是 Aspect Oriented Program (面向切面)的编程的缩写.他是和面向对象编程相对的一个概念.在面向对象的编程中,我们倾向于采用封装.继承.多态等概念,将一个个的功能在 ...

  9. spring-boot中的AOP

    public class User { private Integer id; private String username; private String note; public User(In ...

随机推荐

  1. Vue+ElementUI项目使用webpack输出MPA【华为云分享】

    [摘要] Vue+ElementUI多页面打包改造 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目 ...

  2. 转:Eclipse中创建Maven版的Web工程(详解)

    一.搭建步骤 ♦首先创建一个Maven的Project,如下图: ♦点击Next,勾选 Create a simple project ♦点击Next,注意Packing要选择war,因为我们创建的是 ...

  3. MyCat学习 ------分库分表 随笔

    垂直切分.水平切分 1.垂直分库,解决库中表太多的问题. 2.垂直分表,解决表中列太多的问题.例如 商品表 包含 产地.二维码 .时间.价格.各个列.分为不同的小表. 水平切分, 大数据表拆分为小表 ...

  4. SpringBoot-自动配置原理(七)

    自动配置原理 本节内容分为三个部分 配置文件的写法 分析自动配置原理 @Conditional 一. 配置文件的写法 配置文件可以写什么? 是与/META-INF/spring.factories配置 ...

  5. 手撕 JVM 垃圾收集日志

    下图是本篇的写作大纲,将从以下四个方面介绍怎么样处理 JVM 日志. 有准备才能不慌 想要分析日志,首先你得有日志呀,对不对.凡是未雨绸蒙总是没错的.所谓有日志的意思,你要把 JVM 参数配置好,日志 ...

  6. nbuoj2784 倒水

    题目:http://www.nbuoj.com/v8.83/Problems/Problem.php?pid=2784 一天,TJ买了N个容量无限大的瓶子,开始时每个瓶子里有1升水.接着TJ决定只保留 ...

  7. BZOJ 3107 [cqoi2013]二进制a+b (DP)

    3107: [cqoi2013]二进制a+b Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 995  Solved: 444[Submit][Stat ...

  8. ZOJ-1709

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  9. ARTS-S 获取子线程返回值注意事项

    #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h& ...

  10. unity3d 柏林噪声 PerlinNoise 规律 算法

    测试 每个小数值取100次 print(0.1); LaTest3(0.1f, 0.1f); print("Max:" + La.Max() + "|Min:" ...