SpringBoot:AOP Aspect切面编程
来源:互联网 发布:淘宝店铺怎么装修上新 编辑:程序博客网 时间:2024/05/17 09:25
常用用于实现拦截的有:Filter、HandlerInterceptor、MethodInterceptor
第一种Filter属于Servlet提供的,后两者是Spring提供的,HandlerInterceptor属于Spring MVC项目提供的,用来拦截请求,在MethodInterceptor之前执行。
实现一个HandlerInterceptor可以实现接口HandlerInterceptor,也可以继承HandlerInterceptorAdapter类,两种方法一样。
MethodInterceptor是AOP项目中的拦截器,它拦截的目标是方法,即使不是Controller中的方法。
实现MethodInterceptor拦截器大致也分为两种,一种是实现MethodInterceptor接口,另一种利用Aspect的注解或配置。
关于实现MethodInterceptor接口的这种方法,还需要在配置文件中做配置,在SpringMVC中使用还可以,在SpringBoot中使用起来似乎没有那么方便。
在pom.xml中添加依赖:
新建TestAspect类
import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Aspect@Order(-99) // 控制多个Aspect的执行顺序,越小越先执行@Componentpublic class TestAspect {@Before("execution(public * com.zhangxf.dingcan..*(..))")// 拦截方法;如果你需要拦截指定package指定规则名称的方法,可以使用表达式execution(...) public void beforeTest(JoinPoint point) throws Throwable { System.out.println("before:---->"); } @After("execution(public * com.zhangxf.dingcan..*(..))") public void afterTest(JoinPoint point) { System.out.println("after:----->"); }}然后创建一个Controller验证一下
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.zhangxf.dingcan.pojo.User;@Controllerpublic class HelloController {@RequestMapping("/get")@ResponseBodypublic User get(){User user=new User();user.setBirthday("1998.01.08");user.setUsername("张三");System.out.println("121");return user;}}在浏览器输入http://localhost:8080/get
注意:
1、在application.properties中也不需要添加spring.aop.auto=true,因为这个默认就是true,值为true就是启用@EnableAspectJAutoProxy注解了。
2、你不需要手工添加 @EnableAspectJAutoProxy 注解。
3、当你需要使用CGLIB来实现AOP的时候,需要配置spring.aop.proxy-target-class=true,这个默认值是false,不然默认使用的是标准Java的实现。
其实aspectj的拦截器会被解析成AOP中的advice,最终被适配成MethodInterceptor,这些都是Spring自动完成的,如果你有兴趣,详细的过程请参考springAOP的实现。
关于集中拦截方法的区别总结:
HandlerInterceptoer拦截的是请求地址,所以针对请求地址做一些验证、预处理等操作比较合适。当你需要统计请求的响应时间时MethodInterceptor将不太容易做到,因为它可能跨越很多方法或者只涉及到已经定义好的方法中一部分代码。
MethodInterceptor利用的是AOP的实现机制,在本文中只说明了使用方式,关于原理和机制方面介绍的比较少,因为要说清楚这些需要讲出AOP的相当一部分内容。在对一些普通的方法上的拦截HandlerInterceptoer就无能为力了,这时候只能利用AOP的MethodInterceptor。
Filter是Servlet规范规定的,不属于spring框架,也是用于请求的拦截。但是它适合更粗粒度的拦截,在请求前后做一些编解码处理、日志记录等。
- SpringBoot:AOP Aspect切面编程
- AOP面向切面编程(Aspect Oriented Programming)
- AOP( aspect-oriented programming)面向切面编程
- java 切面编程 AOP(Aspect oriented program)
- AOP(Aspect Oriented Programming),面向切面编程
- SpringBoot 面向切面编程AOP
- SpringBoot(七)Aspect切面
- Spring AOP编程中--@aspect 标签,切面编程
- 三、面向切面编程AOP(Aspect oriented Programming)
- 面向切面编程(AOP:Aspect Orient Programming)1
- 面向切面编程(AOP:Aspect Orient Programming)2
- spring面向切面编程AOP(Aspect-orented programming)
- spring 基于Aspect和注解的切面编程(aop)
- AOP(面向切面编程 Aspect Oriented Programming)
- 什么是Aspect Oriented Programming(AOP)面向切面编程?
- Spring aop(Aspect Oriented Programming:面向切面编程)入门
- 自定义注解 Aspect 实现aop 日志记录切面编程
- Java 面向切面编程(Aspect Oriented Programming,AOP)
- jquery指定输入框获得焦点
- canvas系列教程04-柱状图项目2
- ffmpeg 的那些坑
- scau 9716 矩形的并
- 顺序容器初识
- SpringBoot:AOP Aspect切面编程
- CentOs7下搭载分布式Hadoop2.9
- linux添加新磁盘和创建分区
- xmind破解方法
- python3 连接 mysql 遇到乱码问题解决方案
- css如何让两个并列的按钮 水平居中显示
- canvas系列教程05-柱状图项目3
- springboot 基于 maven 自定义格式打包
- gradle编译: Plugin with id 'java-library' not found.