springboot中aop(注解切面)应用
来源:互联网 发布:域名解析软件的作用 编辑:程序博客网 时间:2024/06/03 17:28
aop的理解:我们传统的编程方式是垂直化的编程,即A–>B–>C–>D这么下去,一个逻辑完毕之后执行另外一段逻辑。但是AOP提供了另外一种思路,它的作用是在业务逻辑不知情(即业务逻辑不需要做任何的改动)的情况下对业务代码的功能进行增强,这种编程思想的使用场景有很多,例如事务提交、方法执行之前的权限检测、日志打印、方法调用事件等等(http://www.importnew.com/26951.html)
1.添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2.切面类.
(1)指定切点
(2)这里的涉及的通知类型有:前置通知、后置最终通知、后置返回通知、后置异常通知、环绕通知,在切面类中添加通知
package com.fcc.web.aop;import com.fcc.domain.annotation.MyInfoAnnotation;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Component;//描述切面类@Aspect@Componentpublic class TestAop { /* * 定义一个切入点 */ @Pointcut("@annotation(com.fcc.domain.annotation.MyInfoAnnotation)") public void myInfoAnnotation() { } // 用@Pointcut来注解一个切入方法 @Pointcut("execution(* com.fcc.web.controller.*.**(..))") public void excudeController() { } /* * 通过连接点切入 */// @Before("execution(* findById*(..)) &&" + "args(id,..)")// public void twiceAsOld1(Long id) {// System.out.println("切面before执行了。。。。id==" + id);//// } //@annotation 这个你应当知道指的是匹配注解 //括号中的 annotation 并不是指所有自定标签,而是指在你的注释实现类中 *Aspect 中对应注解对象的别名,所以别被俩 annotation 所迷惑。 @Around(value ="myInfoAnnotation()&&excudeController()&&@annotation(annotation)") public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint, MyInfoAnnotation annotation ) { System.out.println(annotation.value()); return null; }}
2.用以指定切点的注解
package com.fcc.domain.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD,ElementType.PARAMETER})public @interface MyInfoAnnotation { String value() default "";}3.测试类
@Controller@RequestMapping("business")public class BusinessController { @MyInfoAnnotation(value = "FCC") @RequestMapping("/test") @ResponseBody public String testBusiness(){ return "business"; }}浏览器请求 localhost:8080/business/test
控制台输出:FCC注意:这里用到了JoinPoint。通过JoinPoint可以获得通知的签名信息,如目标方法名、目标方法参数信息等。ps:还可以通过RequestContextHolder来获取请求信息,Session信息。
阅读全文
0 0
- springboot中aop(注解切面)应用
- 面向切面编程(AOP)应用,日志切面,基于注解
- Springboot 自定义注解 AOP切面获取操作日志
- SpringBoot 面向切面编程AOP
- SpringBoot:AOP Aspect切面编程
- aop注解 自定义切面的注解写法
- aop注解 自定义切面的注解写法
- aop注解 自定义切面的注解写法
- SpringBoot学习(四)—AOP切面编程
- spring中面向切面编程AOP的简单应用
- spring aop, aspectj 注解声明切面
- 注解方式实现AOP切面编程
- Spring 面向切面(AOP)编程,注解
- 注解方式实现aop事务切面
- spring AOP切面,注解实现,获取参数
- Spring AOP切面基于注解基础学习
- spring-aop---AspectJ注解式切面编程。
- Spring容器切面编程 aop注解开发 五中通知类型
- 【linux命令】嵌入式linux截屏命令 gsnap
- 面向对象案例(以面向对象的方式开发简易计算器)
- 【Mybatis从0到1-012】多对多查询(resultMap)
- Spring配置问题
- 关于页面请求返回json数据问题
- springboot中aop(注解切面)应用
- C
- 命令行操作jar可执行文件进行excel转json操作并通过命令行传递文件路径
- 关于Ogre的编译
- 第六篇:Runnable和Thread实现多线程的区别(含代码)
- LDO稳压器工作原理
- CURL
- Hibernate-多对多关系映射
- Android Webview js 简单应用