springboot-AOP

先了解AOP的相关术语:

1.通知(Advice):

通知定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。

2.连接点(Joinpoint):

程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。

3.切入点(Pointcut)

通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,Spring中允许我们方便的用正则表达式来指定

4.切面(Aspect)

通知和切入点共同组成了切面:时间、地点和要发生的“故事”

5.引入(Introduction)

引入允许我们向现有的类添加新的方法和属性(Spring提供了一个方法注入的功能)

6.目标(Target)

即被通知的对象,如果没有AOP,那么它的逻辑将要交叉别的事务逻辑,有了AOP之后它可以只关注自己要做的事(AOP让他做爱做的事)

7.代理(proxy)

应用通知的对象,详细内容参见设计模式里面的代理模式

8.织入(Weaving)

把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:
(1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器
(2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码
(3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术

经典的基于代理的AOP:

Spring支持五种类型的通知:

  • @Before(前) org.apringframework.aop.MethodBeforeAdvice
  • @After-returning(返回后) org.springframework.aop.AfterReturningAdvice
  • @After-throwing(抛出后) org.springframework.aop.ThrowsAdvice
  • @Arround(周围) org.aopaliance.intercept.MethodInterceptor
  • @Introduction(引入) org.springframework.aop.IntroductionInterceptor

springboot中使用AOP

在springboot 中使用aop 主要有以下步骤
* 导入依赖
* 创建注解
* 编写切面
* 使用注解

添加依赖

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

创建注解

package com.abel.aop;

import java.lang.annotation.*;

/**
 * Created by yangyibo on 17/7/9.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogRecord {
    String key() default "";
}

编写切面

package com.abel.aop;

import com.abel.model.User;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


/**
 * Created by yangyibo on 17/7/6.
 */

@Aspect
@Component
public class LogInterceptor {

    //定义一个切入面,也就是一个切入点的集合
    //可以用 * 匹配任何类型 例如虾面: 返回值为任何类型的com.abel.service包下的任何类的 add 方法。
    @Pointcut("execution(public * com.abel.service..*.add(..))")
    public void myMethod() {
    }

    ;

    //定义一个注解方式的切入面
    //也就是方法上有@LogRecord 注解的都会被拦截
    @Pointcut("@annotation(com.abel.aop.LogRecord)")
    public void annotationPointCut() {
    }

    ;

    // 方法执行之前执行,也就是在spring管理的对象的方法前执行前执行此方法
    // execution 切入点语法
//    @Before("execution(public void com.abel.dao.impl.UserDAOImpl.save(com.abel.model.User ))")
//拦截方法上有@LogRecord 注解,且 方法入参包含user的方法
    @Before("annotationPointCut() && args(user,..)")
    public void beforeMethod(JoinPoint point, User user) {
        //切入点是加注释的那个方法
        System.out.println("Method : " + point.getSignature().getName());
        //方法参数集合
        Object[] s = point.getArgs();
        System.out.println("Method args : " + s[0].toString());
        System.out.println("start begin .........." + "user :" + user.getUsername());
    }

    //    @AfterReturning("execution(public void com.abel.dao..*.*(..))")
    //AfterReturning 是目标方法执行且返回后执行, rvt 方法的返回值,
    @AfterReturning(value = "annotationPointCut()", returning = "rvt")
    public void afterMethod(Object rvt) {
        //此处获取返回值为null,因为 @Around 的 advice 执行方法后没有把方法的返回值 返回,所以在AfterReturning 中获取的返回值为null
        System.out.println("start AfterReturning ....... and return : " + rvt);
    }

    //@annotation(log) 获取注解中的参数  @LogRecord(key="add is user")
    @Around("annotationPointCut()&& @annotation(log)")
    public void aroundMethod(ProceedingJoinPoint pjp, LogRecord log) throws Throwable {
        System.out.println("method around start ... LogRecord  key : " + log.key());
        //获取目标对象的返回值,打印返回值,但是我不return ,AfterReturning 拿不到 返回值,哈哈哈
        Integer i = (Integer) pjp.proceed();

        System.out.println("method around end ... and method return :" + i);
    }
}

使用注解


import com.abel.aop.LogRecord;
import com.abel.dao.UserDAO;
import com.abel.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDAO userDAO;

    //key 为LogRecord 注解的属性
    @LogRecord(key="add is user")
    public Integer add(User user) {
        userDAO.save(user);
        return 40;
    }
}

springAOP部分
完整代码:https://github.com/527515025/mySpring

本文参考自:http://blog.csdn.net/qq_30747531/article/details/52106365

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值