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. CSS自定义默认样式

    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, ...

  2. obs命令行工具obsutil的使用测试

    test1 批量复制,目标文件夹ggggg-zyx0809/data/tmp/a0文件夹已存在,不使用flat命令,目标路径包含a0文件夹 操作 从ggggg-zyx0809/data/g_cageg ...

  3. 华为参与《基于5G技术的医院网络建设标准》的制定

    [摘要] 5G 千兆网承载五地远程会诊,现场完成三例复杂性疑难重症远程病例讨论 [中国,北京,2019年9月4日] 金秋之际,在国家卫生健康委指导下,由中日友好医院•国家远程医疗与互联网医学中心•国家 ...

  4. postgresql,postgis,geoserver 发布地图服务,并用.net mvc openlayers3进行显示

    1.所需工具 postgres版本 9.6.1 对应的postgis geoserver 2.8.2 openlayers3 2.将postgres postgis ,geosever安装好,再用如下 ...

  5. 转:java 解析excel,带合并单元的excel

    收集了一些对博主有帮助的博文,如下 >>>>>>>>>>>第一部分: 首先,mavn导入jar包 <!-- 解析excel需要导 ...

  6. C#——Unity事件监听器

    事件监听器 事件类型 public enum BaseHEventType { GAME_OVER, GAME_WIN, PAUSE, ENERGY_EMEPTy, GAME_DATA } 事件基类 ...

  7. 牛客竞赛-Who killed Cock Robin

    Who killed Cock Robin? I, said the Sparrow, With my bow and arrow,I killed Cock Robin. Who saw him d ...

  8. Rancher1-简单介绍-认识rancher

    认识rancher 一.简介 1.什么rancher Rancher是一个开源软件平台,使组织能够在生产中运行和管理Docker和Kubernetes.使用Rancher,组织不再需要使用一套独特的开 ...

  9. CodeForces-508A~D篇 div.2

    链接:https://codeforc.es/contest/1038 A题: #include<bits/stdc++.h> using namespace std; typedef l ...

  10. Python流程控制之循环结构

    目录 while循环 for循环 嵌套循环 break.continue.pass 练习 当出现有规律或者是重复的事情就可以使用循环. 1.循环变量初始化 2.循环条件 3.循环体 4.改变循环变量 ...