2507-AOP- springboot中使用-使用注解方式
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中使用-使用注解方式的更多相关文章
- springboot中的常用注解
springboot中的常用注解个人觉得springboor中常用的注解主要可以分为三种:放入容器型注解.从容器中取出型注解和功能型注解.其中的放入容器型和从容器中取出型就是我们平时所说的控制反转和依 ...
- Jeecg中通过Spring_AOP+注解方式实现日志的管理
转载;https://blog.csdn.net/ma451152002/article/details/77234236 Jeecg中通过Spring_AOP+注解方式实现日志的管理 一.设计思路 ...
- springboot中的controller注解没有生效
springboot中的controller注解没有生效 , 启动的Application类没有在controller的父目录或同级目录
- 【归纳】springboot中的IOC注解:注册bean和使用bean
目前了解的springboot中IOC注解主要分为两类: 1. 注册bean:@Component和@Repository.@Service.@Controller .@Configuration 共 ...
- SpringBoot 中定时执行注解(@Scheduled、@EnableScheduling)
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler 接口. ...
- SpringBoot集成websocket(java注解方式)
第一种:SpringBoot官网提供了一种websocket的集成方式 第二种:javax.websocket中提供了元注解的方式 下面讲解简单的第二种 添加依赖 <dependency> ...
- SpringBoot 中 get/post 请求处理方式,以及requestboy为Json时的处理
GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...
- SpringBoot 中使用shiro注解使之生效
在shiroConfig配置类中增加如下代码: /** * 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro ...
- springboot中使用自定义注解实现策略模式,去除工厂模式的switch或ifelse,实现新增策略代码零修改
前言 思路与模拟业务 源码地址 https://gitee.com/houzheng1216/springboot 整体思路就是通过注解在策略类上指定约定好的type,项目启动之后将所有有注解的typ ...
随机推荐
- vscode修改括号对颜色,自定义括号颜色
新版的vscode 1.67(2022年4月更新的版本),自带括号颜色匹配,十分的方便. 至于怎么开启,已经有人写过,这里就不写了,更新到新版默认开启~ 括号颜色默认只有3种颜色,有时候感觉不够用. ...
- vue生命周期加载顺序
1.beforeCreate(创建前)表示实例完全被创建出来之前,vue 实例的挂载元素$el和数据对象 data 都为 undefined,还未初始化.此钩子函数不能获取到数据,dom元素也没有渲染 ...
- mysql查询关键字补充与多表查询
目录 查询关键字补充 having过滤 distinct去重 order by排序 limit分页 regexp正则 多表查询 子查询 连表查询 查询关键字补充 having过滤 关键字having和 ...
- TKE qGPU 通过 CRD 管理集群 GPU 卡资源
作者 刘旭,腾讯云高级工程师,专注容器云原生领域,有多年大规模 Kubernetes 集群管理经验,现负责腾讯云 GPU 容器的研发工作. 背景 目前 TKE 已提供基于 qGPU 的算力/显存强隔离 ...
- 20212115朱时鸿实验一《python程序设计》实验报告
------------恢复内容开始------------ #学号20212115 <python程序设计>实验一报告 课程: <python程序设计> 班级:2121 姓名 ...
- dotnet-cnblog-tool 图片上传失败问题
dotnet-cnblog-tools 这个工具是将本地的 Markdown 文件转换为 可以上传到 cnblog 的格式,并且会将图片自动上传到 cnblog 的图床. 具体可以参考这篇文章: cn ...
- Vue自定义组件之v-model的使用
自定义组件之v-model的使用 v-model的语法糖可以为下面v-bind && @input联合完成: <input v-model="text"> ...
- 接口测试postman深度挖掘应用③--postman终结篇
上一章节我们介绍了postman的变量测试以及导入数据测试基本上技术性的东西已经差不过了,这篇文章再系统性的介绍一下. 一.下载 官网:https://www.postman.com 1.选择需要下载 ...
- 『忘了再学』Shell流程控制 — 33、if条件判断语句(一)
目录 1.单分支if条件语句 2.双分支if条件语句 (1)示例1 (2)示例2 什么是流程控制? 普通理解:Shell编写的程序是顺序执行的,也就是说第一命令先执行,然后接着执行第二条命令,然后再下 ...
- 重学ES系列之模版字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...