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

转载于:https://www.cnblogs.com/starmoon1994/p/9103939.html

2507-AOP- springboot中使用-使用注解方式相关推荐

  1. 难以想象SpringBoot中的条件注解底层居然是这样实现的

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源 | https://urlify.cn/bm2qqi Spr ...

  2. 面试:SpringBoot中的条件注解底层是如何实现的?

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 来源 | https://urlify.cn/bm2qqi Spr ...

  3. springboot中的@Conditional注解

    springboot中使用@Configuration注解完成一个配置类的配置,被该注解标注的类会在程序启动时自动的加载到IOC容器,但如果我们想通过一些条件来控制被@Configuraion注解标注 ...

  4. spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式

    spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式   在类名,接口头上注解使用在 @JsonIgnoreProperties(value={"comid" ...

  5. springboot中使用@Transactional注解事物不生效的原因

    一:在springboot中使用事物遇到的坑 1.我们知道spring中的事物分为两种:一种是编程式事物,一种是声明式事物.顾名思义,编程式事物是指通过代码去实现事物管理,这里不做过多说明.另一种是声 ...

  6. SpringBoot 中使用 @Valid 注解 + Exception 全局处理器优雅处理参数验证

    作者:超级小豆丁 http://www.mydlq.club/article/49/ 目录 一.为什么使用 @Valid 来验证参数 二.@Valid 注解的作用 三.@Valid 的相关注解 四.使 ...

  7. SpringBoot的MyBatis generator 注解方式和xml方式 (四)

    两种形式: XML方式 type="XMLMAPPER" 注解方式 type="ANNOTATEDMAPPER" 1.1 pom.xml里添加maven插件 & ...

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

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

  9. 成都大数据培训之SpringBoot中关于JDBC的方式运用

    在成都大数据培训中,Springboot中对于数据访问层,无论是SQL还是NOSQL,都默认采用整合Spring Data的方式进行统一处理,Springboot会帮我们添加大量自动配置,屏蔽了很多设 ...

  10. springboot中mybatisplus基于注解的多对多级联查询

    mybatisplus使用注解多对多级联查询 mybatis提供了注解和xml两种方式配置我们的sql语句,我在接触使用的过程中更喜欢注解的方式,在我的上一个项目中,我全部使用mybatis注解去完成 ...

最新文章

  1. 感谢大家对我微软TECHED2013课程的支持
  2. postfix过滤器名称不一致导致postfix/qmg:warning: connect to transport xxxx: No such file or directory...
  3. 【算法导论33】跳跃表(Skip list)原理与java实现
  4. 采访前高级软件工程课程学员付浩同学,及Scrum总结阅读感想
  5. 《无线网络技术教程第二版》阅读笔记(一)
  6. 百度输入法、QQ 浏览器竟都在窃取用户隐私?
  7. 小程序切换账户拉取仓库文件的appid提示
  8. Arcgis server——arcgis server manager忘记密码
  9. CDN是什么?一分钟带你了解CDN
  10. mb是做1还是0_新手爸妈看过来:0-1岁宝宝这样做早教,省钱省心又实用
  11. Android studio使用SVN
  12. C语言学习笔记(自用)(1):初识C语言
  13. Excle 取前几位数、中间几位数、后几位数的方法
  14. 加菲猫语录精选74 中英俄三语版
  15. 苏宁易购商品详情API接口
  16. 怎样做好一个服务器管理员?
  17. 单板嵌入式计算机定义,用于嵌入式控制系统的单板计算机
  18. springmvc-kuang
  19. 《数学之旅》及《什么是数学》
  20. 【2020可用】Python使用 imaplib imapclient连接网易邮箱提示 Unsafe Login. Please contact kefu@188.com for help 的解决办法

热门文章

  1. WSS3.0开发-过滤列表内容(2)--一个增强的列表元数据查询webpart(SmartQueryWebPart)...
  2. element table多选和单选
  3. linux各路径(目录)的解释(转载)
  4. SpringAOP 学习笔记
  5. linux服务器数据备份到本地硬盘_如何备份硬盘数据,最简单的方法是什么?
  6. FISCO BCOS(一)———搭建单群组FISCO BCOS联盟链
  7. hive设置为本地模式,从而避免MapReduce
  8. web文件被更改crawlergo怎么解决_hscan:一款集成crawlergo和xray的src漏洞挖掘利器
  9. linux卸载文件挂载,磁盘文件挂载与卸载
  10. python可以实现那些功能_30 个Python代码实现的常用功能,精心整理版