springboot中aop(注解切面)应用
2017-08-22 09:28
363 查看
aop的理解:(http://www.importnew.com/26951.html)
1.添加依赖
2.切面类.
(1)指定切点
(2)这里的涉及的通知类型有:前置通知、后置最终通知、后置返回通知、后置异常通知、环绕通知,在切面类中添加通知
2.用以指定切点的注解
控制台输出:FCC
注意:这里用到了JoinPoint。通过JoinPoint可以获得通知的签名信息,如目标方法名、目标方法参数信息等。ps:还可以通过RequestContextHolder来获取请求信息,Session信息。
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 @Component public 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信息。
相关文章推荐
- 面向切面编程(AOP)应用,日志切面,基于注解
- 12-26 AOP面向切面(XML实现和注解实现)
- Spring4-AOP切面-注解方式
- Spring aop切面实际应用 XML配置
- spring aop使用注解从切面中获取参数
- 注解方式实现AOP切面编程
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring2.0 AOP @AspectJ注解定义切面学习示例。
- Spring学习4-面向切面(AOP)之aspectj注解方式
- SpringBoot 基于@AspectJ的切面编程(AOP)
- Java乔晓松-基于注解的面向AOP(切面)编程
- Spring学习4-面向切面(AOP)之aspectj注解方式
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- AOP如何对自定义注解进行切面编程
- AOP 切面的使用,以及如何在通知上获取切入方法的注解和参数
- Spring(十一)使用Spring的注解方式实现面向切面技术AOP
- Spring入门4 静态代理 动态代理 CGLIB AOP切入点切面 AOP注解
- 利用自定义注解 为个别的接口做AOP切面
- Spring---AOP基本概念以及Advice5种类型的通知注解应用实例
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)