Spring Aop实现

AOP简介

AOP (Aspect Orient Programming),直译过来就是 面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成各个层次的对象,而面向切面编程是将程序抽象成各个切面,是Spring的核心思想之一。

AOP实现

Spring中的aop是通过动态代理实现的,那么他具体是如何实现的呢?Spring通过一个切面类,在他的类上加入@Aspect 注解,定义一个Pointcut方法,最后定义一系列的增强方法。这样就完成一个对象的切面操作。

使用注解实现Spring AOP

Spring AOP通知类型

  • @Before:前置通知,在目标方法执行之前运行的通知,但不能阻止执行流继续到连接点(除非抛出异常)
  • @AfterReturning:返回后通知,在目标方法正常返回之后运行的通知(即一个方法正常返回而且没有抛出异常)
  • @AfterThrowing:抛出异常后通知,在目标方法通过抛出异常退出,则运行AfterThrowing通知
  • @After:后置通知,在目标方法运行结束之后,不管目标是否正常退出或者异常返回都将运行的通知
  • @Around:环绕通知,环绕目标方法的通知,环绕通知可以在方法调用之前和之后执行自定义行为

AOP注解

  • @Aspect:作用是把当前类标识为一个切面供容器读取
  • @Pointcut:切入点,可以定义表达式或者方法签名
  • @EnableAspectJAutoProxy:用于开启Spring Aop注解的功能

service接口类

public interface TestService {
    void test();
}

service实现类将实现类注入到Spring容器中

@Service
public class TestServiceImpl implements TestService {
    @Override
    public void test() {
        System.out.println("TestService的Test()方法执行");
    }
}

创建一个controller其中把service类注入到controller中,同时把controller注册到ioc容器中

@Component("TestController")
public class TestController {

    @Autowired
    private TestService testService;

    public void Test(){
        testService.test();
    }
}

定义一个切面类并且注册到ioc容器中

切入点表达式看你项目实现类的package

//切面类
@Aspect
@Component
public class TestAspect {

    //定义切入点
    @Pointcut("execution(* com.example.springaop.service.impl..*.*(..))")
    private void pointcut(){

    }

    //环绕通知
    @Around("pointcut()")
    //ProceedingJoinPoint 连接点对象
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Exception {
        //获取签名
        Signature signature = proceedingJoinPoint.getSignature();
        //获取目标方法名称
        String methodName = signature.getName();
        System.out.println("开始执行环绕通知,方法名称"+ methodName);
        try{
            long startTime = System.currentTimeMillis();
            //执行实现类test方法
            Object proceed = proceedingJoinPoint.proceed();
            long endTime = System.currentTimeMillis();
            System.out.println(methodName + "方法耗时:" + (endTime - startTime) + "毫秒");
            return proceed;
        }catch (Throwable throwable){
            throwable.printStackTrace();
            throw new Exception("方法异常");
        }
    }


    //前置通知
    @Before("pointcut()")
    public void before(JoinPoint joinpoint){
        String methodName = joinpoint.getSignature().getName();
        System.out.println("开始执行前置通知,方法名称"+ methodName);
    }

    //后置通知
    @After("pointcut()")
    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("开始执行后置通知,方法名称"+ methodName);
    }

    //返回通知
    @AfterReturning("pointcut()")
    public void afterReturning(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("开始执行后置返回通知,方法名称"+ methodName);
    }

    //异常通知
    @AfterThrowing("pointcut()")
    public void afterThrowing(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("开始执行抛出异常返回通知,方法名称"+ methodName);
    }

}

定义一个aop config配置类

@Configuration
//开启Aop注解
@EnableAspectJAutoProxy
@ComponentScan("com.example.springaop")
public class AopConfig {
}

测试运行

@Test
void contextLoads() {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AopConfig.class);
    TestController testController = (TestController) annotationConfigApplicationContext.getBean("TestController");
    testController.Test();
}

在这里插入图片描述
模拟异常情况
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP(面向切面编程)是Spring框架中的一个重要模块,它提供了一种在程序运行期间动态地将额外的行为织入到代码中的方式。通过使用Spring AOP,我们可以将与业务逻辑无关的横切关注点(如日志记录、性能统计、事务管理等)从业务逻辑中分离出来,使得代码更加清晰、可维护和可扩展。 Spring AOP实现主要依赖于以下几个核心概念: 1. 切面(Aspect):切面是一个模块化的单元,它封装了与横切关注点相关的行为。在Spring AOP中,切面可以包含通知(Advice)和切点(Pointcut)。 2. 通知(Advice):通知定义了在切面的特定位置执行的代码。在Spring AOP中,有以下几种类型的通知: - 前置通知(Before):在目标方法执行之前执行。 - 后置通知(After):在目标方法执行之后执行,无论是否发生异常。 - 返回通知(After-returning):在目标方法正常返回之后执行。 - 异常通知(After-throwing):在目标方法抛出异常后执行。 - 环绕通知(Around):包围目标方法的执行,在前后都可以添加额外的逻辑。 3. 切点(Pointcut):切点定义了在哪些连接点(Joinpoint)上应用通知。通过使用切点表达式,我们可以指定需要拦截的方法或类。 4. 连接点(Joinpoint):连接点是在应用程序执行过程中能够插入切面的点,如方法调用、异常抛出等。 5. 织入(Weaving):织入是将切面应用到目标对象并创建代理对象的过程。Spring AOP支持编译时织入、类加载时织入和运行时织入三种方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值