Springboot中使用aop,与SSM中使用AOP,整体配置与编写方式都是类似的。但是Springboot简化了很多xml配置,切点的表达式可以直接进行javaconfig。

记录一些示例

springboot示例:

版本1.5.9.RELEASE

pom文件中添加aop的依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

自定义注解:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* Target指定注解的目标为方法级
* Retention指定注解可以在运行时被获取(利用反射)
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CountAopAnnotation {
}

aop类:


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; /**
* aop
* Created by hxy 2018/5/5.
*/ @Component
@Aspect
@Order(10) //构建执行顺序
public class CountAopHelper { /*
* 定义一个切入点
*/
@Pointcut("@annotation(com.company.project.core.annotation.CountAopAnnotation)")
public void myInfoAnnotation() {
} // 用@Pointcut来注解一个切入方法
@Pointcut("execution(* com.company.project.web.*.*(..))")
public void excudeController() {
} @Before("myInfoAnnotation()")
public void deBefore(JoinPoint joinPoint) throws Throwable {
System.out.println("deBefore"); // 通过RequestContextHolder获取HttpServletRequest 可以获取请求头相关的内容
// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// StringBuffer requestURL = request.getRequestURL();
} @After("myInfoAnnotation()")
public void doAfter(JoinPoint joinPoint) throws Throwable {
System.out.println("doAfter"); // 通过RequestContextHolder获取HttpServletRequest 可以获取请求头相关的内容
// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// StringBuffer requestURL = request.getRequestURL();
} /**
* Around(环绕通知)是在Before(前置通知)前面执行
* &&@annotation(annotation) 这个是对方法参数的形参进行注入
* <p>
* value可以是多种 1纯注解形式 myInfoAnnotation() 2 混合 myInfoAnnotation()&&@annotation(annotation)&& excudeController()
* 使用场景 1大面积使用aop 使用Pointcut来写匹配表达式 2精准定位 使用注解形式
*/
@Around(value = "myInfoAnnotation()")
public Object doAround(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println("doAround"); // 获取切点的参数
Object[] args = thisJoinPoint.getArgs(); //环绕通知必须执行,否则不进入注解的方法
return thisJoinPoint.proceed();
} }

使用:

在接口上加上注解

   @CountAopAnnotation
@GetMapping("/testAnno")
public Result testAnno() { System.out.println("here is api testAnno"); return ResultGenerator.genSuccessResult("dsds");
}

执行结果是:

先执行了环绕通知,再执行前置通知,再执行被aop代理的方法,再执行后置通知;

doAround
deBefore
here is api testAnno
doAfter

2507-AOP- springboot中使用-使用注解方式的更多相关文章

  1. springboot中的常用注解

    springboot中的常用注解个人觉得springboor中常用的注解主要可以分为三种:放入容器型注解.从容器中取出型注解和功能型注解.其中的放入容器型和从容器中取出型就是我们平时所说的控制反转和依 ...

  2. Jeecg中通过Spring_AOP+注解方式实现日志的管理

    转载;https://blog.csdn.net/ma451152002/article/details/77234236 Jeecg中通过Spring_AOP+注解方式实现日志的管理 一.设计思路 ...

  3. springboot中的controller注解没有生效

    springboot中的controller注解没有生效  , 启动的Application类没有在controller的父目录或同级目录

  4. 【归纳】springboot中的IOC注解:注册bean和使用bean

    目前了解的springboot中IOC注解主要分为两类: 1. 注册bean:@Component和@Repository.@Service.@Controller .@Configuration 共 ...

  5. SpringBoot 中定时执行注解(@Scheduled、@EnableScheduling)

    项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler 接口. ...

  6. SpringBoot集成websocket(java注解方式)

    第一种:SpringBoot官网提供了一种websocket的集成方式 第二种:javax.websocket中提供了元注解的方式 下面讲解简单的第二种 添加依赖 <dependency> ...

  7. SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理

    GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...

  8. SpringBoot 中使用shiro注解使之生效

    在shiroConfig配置类中增加如下代码: /** * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro ...

  9. springboot中使用自定义注解实现策略模式,去除工厂模式的switch或ifelse,实现新增策略代码零修改

    前言 思路与模拟业务 源码地址 https://gitee.com/houzheng1216/springboot 整体思路就是通过注解在策略类上指定约定好的type,项目启动之后将所有有注解的typ ...

随机推荐

  1. windbg的时间旅行实现对 C# 程序的终极调试

    一:什么是时间旅行 简而言之就是把程序的执行流拍成vlog,这样就可以对 vlog 快进或者倒退,还可以分享给别人做进一步的分析,是不是想都不敢想. 很开心的是 windbg preview 版本中已 ...

  2. arthas学习图文记录

    Arthas 是阿里开源的 Java 诊断工具.在线排查问题,无需重启:动态跟踪 Java 代码:实时监控 JVM 状态.Arthas 支持 JDK 6+,支持 Linux/Mac/Windows,采 ...

  3. Java基础(1)——ThreadLocal

    1. Java基础(1)--ThreadLocal 1.1. ThreadLocal ThreadLocal是一个泛型类,当我们在一个类中声明一个字段:private ThreadLocal<F ...

  4. 【HEOI2014】大工程<虚树>

    虚树 我们每天都用心思索着,这究竟是为了什么呢?我想我也不知道,只是觉得如果人不思考问题就很无聊. 我觉得虚树不是什么数据结构,就是一种技巧或者工具.它能把树中\(k\)个关键点以\(O(klogk) ...

  5. nazo.io 通关记录

    游戏网址 说在前面 答案错误页面 nazo.io/wrong 攻略 第0关 谜.io 纯粹是欢迎你来游戏. 所以他给你的start就是答案. 第1关 欢迎 它用灰体字写了key: welcome 直接 ...

  6. springboot如何使用自定义配置文件

      从前边<springboot竟然有5种默认的加载路径,你未必都知道>我们知道,springboot会默认加载application.properties/application.yml ...

  7. Android Proguard混淆对抗之我见

    关于何为Proguard,可以参考GuardSquare官网其优化业务及Wikipedia相关条目. Proguard:https://www.guardsquare.com/proguard Wik ...

  8. 本地创建的jupyter notebook 无法连接本地环境(即不能运行代码)

    参考:https://www.cnblogs.com/damin1909/p/12691147.html 本人所用的python是anaconda下的,由于需求不同,创建了好多个python用于不同的 ...

  9. Spring cloud gateway 如何在路由时进行负载均衡

    本文为博主原创,转载请注明出处: 1.spring cloud gateway 配置路由 在网关模块的配置文件中配置路由: spring: cloud: gateway: routes: - id: ...

  10. CVE-2021-3156漏洞复现

    CVE-2021-3156linux sudo 权限提升 版本ubantu18.04 使用这个命令可以是普通用户直接提升至管理员权限. 手动测试终端输入 sudoedit -s / 不知道什么原因ub ...