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 ...
随机推荐
- 虚拟机:ESX
VMware ESXi 与ESX 产品之比较 VMware vSphere 5.0 以后版本,所有底层虚拟化产品都改为ESXi产品,本文主要比较了ESXi与ESX的各自特点,以便对大家是否要把现有 ...
- 693. Binary Number with Alternating Bits - LeetCode
Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- 136_Power BI 自定义矩阵热力图
博客:www.jiaopengzi.com 焦棚子的文章目录 请点击下载附件 一.背景 在前几天在pbirs自定义登录后,发现一个问题就是非Power BI原生的视觉无法显示,改回AD登录又可以了.目 ...
- Springmvc基础及应用
SpringMVC简介和环境搭建 SpringMVC简介 Spring 为展现层提供的基于 MVC 设计理念的优秀的Web 框架,是目前最主流的 MVC 框架之一.在Spring3.0 后全面超越 S ...
- python面向对象双下划线方法与元类
目录 双下划线方法(__) 元类简介 产生类的两种表现形式 元类的基本使用 元类进阶操作 __new__方法 双下划线方法(__) 面向对象中的双下方法也有一些人称之为是魔法方法,有些双下方法不需要刻 ...
- MySQL数据库4
内容概要 查询关键字 查询关键字之having过滤 查询关键字之distinct去重 查询关键字之order by排序 查询关键字之limit分页 查询关键字之regexp正则 多表查询思路 可视化软 ...
- plt.figure()的使用,plt.plot(),plt.subplot(),plt.subplots()和图中图
参考:https://blog.csdn.net/m0_37362454/article/details/81511427 matplotlib官方文档:https://matplotlib.org/ ...
- Redis 中的原子操作(3)-使用Redis实现分布式锁
Redis 中的分布式锁如何使用 分布式锁的使用场景 使用 Redis 来实现分布式锁 使用 set key value px milliseconds nx 实现 SETNX+Lua 实现 使用 R ...
- redis相关知识点
redis 的相关知识点 启动 启动代码 redis-cli -a 密码 通用命令 expire: 设置有效期 expire name 10 key key * 相关数据类型 String set:添 ...
- <%= %> <%- %> <% %>是什么意思?
.ejs文件后缀的数据渲染,这是服务器端的.把 .html改成 .ejs, (1)<%= %>相当于html中的innerTEXT,导出不包含标签 . (2)<%- %>相当于 ...