美文网首页
2. Spring4.x AOP

2. Spring4.x AOP

作者: 第八号灬当铺 | 来源:发表于2017-08-29 10:12 被阅读0次

AOP的基本使用

  1. pom.xml中导入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  1. 新建一个拦截规则的注解
package com.xiaohan.aop;

import java.lang.annotation.*;

/**
 * 拦截规则的注解
 */

@Target(ElementType.METHOD)             // 限制该注解只能使用在方法基本上
@Retention(RetentionPolicy.RUNTIME)     // 保留策略 运行时可见
@Documented
public @interface SysLog {
    String name();
}
  1. 定义被拦截的类
package com.xiaohan.aop;

import org.springframework.stereotype.Service;

//使用 方法规则的 被拦截类
@Service
public class DemoMethodService {

    public void add() {
    }
}
package com.xiaohan.aop;

import org.springframework.stereotype.Service;

//使用 注解的 被拦截类
@Service
public class DemoAnnotionService {

    @SysLog(name = "注解方式拦截的add操作")
    public void add() {
    }
}
  1. 定义切面类
    @Before 前置通知 在目标方法执行之前执行
    @After 后置通知 在目标方法执行之后执行 无论是否发生异常
    @AfterReturning 返回通知 在目标方法返回结果之后执行
    @AfterThrowing 异常通知 在目标方法抛出异常之后通知
    @Around 环绕通知 围绕着目标方法执行
package com.xiaohan.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

//  声明这是一个切面类
@Aspect
@Component
public class LogAspect {

    //  定义拦截规则(切点)
    @Pointcut("@annotation(com.xiaohan.aop.SysLog)")
    public void annotationPointCut() {
    }

    // 使用@Pointcut定义的切点规则
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = method.getAnnotation(SysLog.class);
        System.err.println("后置通知 " + sysLog.name());
    }

    // 使用execution表达式定义的切点规则
    @Before("execution(* com.xiaohan.aop.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.err.println("前置通知 " + "方法规则方式拦截的方法" + method.getName());
    }

    // 环绕通知是唯一一个可以 终止连接点(方法)执行 的通知
    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        System.err.println("环绕通知 拦截" + "注解的" + method.getName() + "方法执行了" + time + "毫秒");
        return result;
    }
}
  1. 新建一个配置类
package com.xiaohan.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.xiaohan.aop")

//开启Spring对AspectJ代理的支持
@EnableAspectJAutoProxy
public class AopConfig {
}
  1. 创建一个Main方法 初始化容器 并测试
package com.xiaohan.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

// AOP
public class Main {
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoMethodService demoMethodService = ac.getBean(DemoMethodService.class);
        demoMethodService.add();
        DemoAnnotionService demoAnnotionService = ac.getBean(DemoAnnotionService.class);
        demoAnnotionService.add();
    }
}

输出

前置通知 方法规则方式拦截的方法add
环绕通知 拦截注解的add方法执行了7毫秒
后置通知 注解方式拦截的add操作

相关文章

  • 2. Spring4.x AOP

    AOP的基本使用 pom.xml中导入依赖 新建一个拦截规则的注解 定义被拦截的类 定义切面类@Before ...

  • 10.Spring中事务控制

    1.基于XML的AOP实现事务控制 2.基于注解的AOP实现事务控制 基于注解的AOP实现事务控制,方便演示,我们...

  • SpringBoot条件注解解析

    @Conditional 和 Condition, spring4.x提供,非springboot专有 https...

  • java-spring-2

    一 学习大纲 1. 动态代理设计模式(JDK和cglib) 2. AOP详解 3. AOP中几种...

  • Spring AOP 详解

    一.AOP1.AOP:中文名称面向切面编程2.英文名称:(Aspect Oriented Programming)...

  • @transactional 失效

    1.是否添加了依赖(Transactional依赖AOP实现,因此需要导入aop相关依赖)2.方法是否是publi...

  • Spring(5) -(15)使用Xml开发AOP

    一.了解AOP相关的术语的概念 二.使用Xml开发AOP 1. 加入依赖的jar包 2.配置(代码演示)

  • spring笔记-AspectJ(AOP)

    AspectJ主要使用注解的方式来实现AOP,Spring AOP偏重编程的方式 1.示例接口 2.定义切面 2....

  • Spring传播行为

    参考资料:[1]. 精通Spring4.X[2]. 事务传播行为Requires new和Required的例子[...

  • Spring注解--AOP原理(一):整体流程

    1. 添加@EnableAspectJAutoProxy注解,开启AOP 2. 在容器中引入 Annotatio...

网友评论

      本文标题:2. Spring4.x AOP

      本文链接:https://www.haomeiwen.com/subject/retodxtx.html