springboot中aop应用

9 篇文章 0 订阅
3 篇文章 0 订阅

aop的理解:我们传统的编程方式是垂直化的编程,即A–>B–>C–>D这么下去,一个逻辑完毕之后执行另外一段逻辑。但是AOP提供了另外一种思路,它的作用是在业务逻辑不知情(即业务逻辑不需要做任何的改动)的情况下对业务代码的功能进行增强,这种编程思想的使用场景有很多,例如事务提交、方法执行之前的权限检测、日志打印、方法调用事件等等http://www.importnew.com/26951.html)

1.添加依赖

[html]  view plain  copy
  1. <dependency>  
  2.             <groupId>org.springframework.boot</groupId>  
  3.             <artifactId>spring-boot-starter-aop</artifactId>  
  4.         </dependency>  


2.切面类.

(1)指定切点

(2)这里的涉及的通知类型有:前置通知、后置最终通知、后置返回通知、后置异常通知、环绕通知,在切面类中添加通知

[java]  view plain  copy
  1. package com.fcc.web.aop;  
  2.   
  3. import com.fcc.domain.annotation.MyInfoAnnotation;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5. import org.aspectj.lang.annotation.Around;  
  6. import org.aspectj.lang.annotation.Aspect;  
  7. import org.aspectj.lang.annotation.Before;  
  8. import org.aspectj.lang.annotation.Pointcut;  
  9. import org.springframework.context.annotation.Configuration;  
  10. import org.springframework.stereotype.Component;  
  11.   
  12.   
  13. //描述切面类  
  14. @Aspect  
  15. @Component  
  16. public class TestAop {  
  17.   
  18.     /* 
  19.      * 定义一个切入点 
  20.      */  
  21.     @Pointcut("@annotation(com.fcc.domain.annotation.MyInfoAnnotation)")  
  22.     public void myInfoAnnotation() {  
  23.     }  
  24.   
  25.   
  26.     // 用@Pointcut来注解一个切入方法  
  27.     @Pointcut("execution(* com.fcc.web.controller.*.**(..))")  
  28.     public void excudeController() {  
  29.     }  
  30.   
  31.     /* 
  32.      * 通过连接点切入 
  33.      */  
  34. //    @Before("execution(* findById*(..)) &&" + "args(id,..)")  
  35. //    public void twiceAsOld1(Long id) {  
  36. //        System.out.println("切面before执行了。。。。id==" + id);  
  37. //  
  38. //    }  
  39.   
  40.   
  41.     //@annotation 这个你应当知道指的是匹配注解  
  42.     //括号中的 annotation 并不是指所有自定标签,而是指在你的注释实现类中 *Aspect 中对应注解对象的别名,所以别被俩 annotation  所迷惑。  
  43.     @Around(value ="myInfoAnnotation()&&excudeController()&&@annotation(annotation)")  
  44.     public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint,  
  45.                              MyInfoAnnotation annotation  
  46.     ) {  
  47.         System.out.println(annotation.value());  
  48.   
  49.         return null;  
  50.     }  
  51.   
  52. }  

2.用以指定切点的注解

[java]  view plain  copy
  1. package com.fcc.domain.annotation;  
  2.   
  3. import java.lang.annotation.ElementType;  
  4. import java.lang.annotation.Retention;  
  5. import java.lang.annotation.RetentionPolicy;  
  6. import java.lang.annotation.Target;  
  7.   
  8. @Retention(RetentionPolicy.RUNTIME)  
  9. @Target({ElementType.METHOD,ElementType.PARAMETER})  
  10. public @interface MyInfoAnnotation {  
  11.     String value() default "";  
  12. }  
3.测试类

[java]  view plain  copy
  1. @Controller  
  2. @RequestMapping("business")  
  3. public class BusinessController {  
  4.   
  5.   
  6.     @MyInfoAnnotation(value = "FCC")  
  7.     @RequestMapping("/test")  
  8.     @ResponseBody  
  9.     public String testBusiness(){  
  10.   
  11.         return "business";  
  12.     }  
  13. }  
浏览器请求  localhost:8080/business/test
控制台输出:FCC

注意:这里用到了JoinPoint。通过JoinPoint可以获得通知的签名信息,如目标方法名、目标方法参数信息等。ps:还可以通过RequestContextHolder来获取请求信息,Session信息。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值